Reputation: 663
Hibernate is a persistence framework which is used to persist data from Java environment to database.
I am so confused.. if we persist an object to the database, then why does Hibernate Architecture depicts the persistent object in the middle of Application and Hibernate in the picture below?
(source: viralpatel.net)
Upvotes: 40
Views: 65363
Reputation: 7643
Persistence object is Java object of a class which is parallel to the DB table.
If you have an EMP
table in DB, with columns name
, age
and salary
. Then you can make a Java class with data fields name
, age
and salary
with their getter
and setter
for all 3 fields.
Object of such classes which has a parallel DB table are called Persistent Object
.
In the given diagram below:
Java Application will have POJO
class which will have Persistent Objects
, then Hibernate
framework will be persisting these objects in DB with the knowedge of DB connections and object <-> table mapping with hibernate.properties
and XML Mapping
respectively.
Upvotes: 0
Reputation: 331
Firstly you need to understand the three states of the Hibernate object i.e Transient, Persistent, Detached.
Transient state: An object is in transient state if it just has been instantiated using the new operator and there is no reference of it in the database i.e it does not represent any row in the database.
Persistent state: An object is in the persistent state if it has some reference in the database i.e it represent some row in the database and identifier value is assigned to it. If any changes are made to the object then hibernate will detect those changes and effects will be there in the database that is why the name Persistent. These changes are made when session is closed. A persistent object is in the session scope.
Detached state: An object that has been persistent and is no longer in the session scope. The hibernate will not detect any changes made to this object. It can be connected to the session again to make it persistent again.
Upvotes: 33
Reputation: 2770
I will make it more clearer. Persistent objects are instances of POJO classes that you create that represent rows in the table in the database. According to hibernate-doc an instance of POJO class representing table in database goes through 3 states of which persistent is one of them.
When a POJO instance is in session scope, it is said to be persistent i.e hibernate detects any changes made to that object and synchronizes it with database when we close or flush the session.
And about hibernate.properties and XML Mapping @Ken Chan is right. Go through hibernate-doc for more illustrations on objects in hibernate.
Upvotes: 37
Reputation: 3652
Persistent Objects are generally those objects that exist in memory even beyond the duration of the process that creates it. These objects are then stored in the database.
Upvotes: 6
Reputation: 51
Persistent object is nothing but an instance of POJO class. And POJO class is nothing but a class that represents a table. And Hibernate always monitoring that persistent object. Whenever you manipulate that object or you made any changes in that object, Hibernate will do the same in that table (that one represent by the POJO class). that all handle by the hibernate. So in sort this is the one of the feature of hibernate framework.
Upvotes: 5
Reputation: 90447
According to the figure , you configure hibernate.properties
or some XML mapping
to map a database table to a java object which is called persistent object
.
Then in your application
, you use the persistent object
as a normal java object to
manipulate its state . You can pass persistent object
to hibernate .Hibernate will then generate and issue the necessary SQL to DB to synchronize the state of the persistent object
and its corresponding database record .Does it make sense ?
Upvotes: 8
Reputation: 1197
Persistant objects are the classes that in your program that has a representation in the database.
Example if you have a Car class with the properties NumberPlate, Fuel. The NHibernate configuration will map this to for example a table in the database that is named Car and has these columns corresponding to the Car class properties.
Upvotes: 2