Now we are done with our Simple Example, we should be moving to a more Advanced eExample,
but first you need to know the different 'States of objects' in a Hibernate application.
Through out the previous part of the tutorial, we have been using the words 'Persistent' objects
and 'Persistent' Classes. So what are persistent objects and what are other types of objects ?
Let's first define 'Persistence'...
According to wikipedia, Persistence "refers to the characteristic of data that outlives the execution
of the program that created it".
So, for example,
If you are using a traditional calculator software, the data used in the application is not persistent,
because once the application is closed, the data is not there any more (i.e. Data did not outlive
"the execution of the program that created it").
But if your software saves the calculations in a database for example, this data is persistent (it
"outlives the execution of the program that created it")
So accordingly, persistent objects are those objects whose data is not lost when the application is
closed (i.e. There exist database records containing the exact values of the objects' variables).
Back to Hibernate objects' states; They are three states:
-
Persistent state.
The object is gauranteed to be representing a database record. These are objects attached to a
Hibernate session. They are objects either saved , retrived ... etc by Hibernate.
In our example, the object lecturer is persistent after this statment
session.save(lecturer1)
-
Transient state.
Those are normal objects, used through out the application, and lose there values once the
application is closed.
In our example, the object lecturer is transient before this statment
session.save(lecturer1)
-
Detached objects.
They are objects that where once persistent (attached to a certain context), but then this
context is closed. It is not guranteed that this object is reflecting the database state.
In our example, the object lecturer is detached after the statment
tx.commit()
Test it yourself:
-
Print the ID of the object lecturer1
-
Before session.save(lecturer1);
-
After session.save(lecturer1);
-
Add lecturer1.setLastName("MEAWAD");
-
Between session.save(lecturer1); and tx.commit().
-
After tx.commit();
Watch what happens and analize it yourself.
Read about
Hibernate Objects' states at JBoss comminity Documentation.
Keep these terms in mind, because we are using them in the Advanced Example next section.