Life Cycle
Actually POJO class object has following three states like-
Transient state
Persistent state
Detached state
To Insert:
Session sec=fact.openSession();
sec.save(obj);
To Fetch:
Object o=sec.load(Pojo class.newInteger(101);
Pojo obj=(Pojo)o;
obj.getName();
To Delete:
Object o=sec.load(Pojo class.newInteger(101);
Pojo obj=(Pojo)o;
sec.delete(obj);
To Update:
Object o=sec.load(Pojo class.newInteger(101);
Pojo obj=(Pojo)o;
obj.setId(104); obj.setName("abc"); tx.commit();
Transient & Persistent states:
> Whenever an object of a pojo class is created then it will be in the Transient state.
> When the object is in a Transient state it doesn't represent any row of the database, i.e. it is not associated with any Session object.
> If we modify the data of a pojo class object, when it is in transient state then it doesn't effect on the database table.
> When the object is in persistent state, then it represents one row of the database, if the object is in persistent state then it is associated with the unique Session.
> If we want to move an object from persistent to detached state, we need to do either closing that session or need to clear the cache of the session.
> If we want to move an object from persistent state into transient state then we need to delete that object permanently from the database.
1. In the above client program, line numbers 16 to 19 we just loaded the object and called the corresponding setter methods, it is not related to the database row.
2. If you see, line number 24 we called save method in the Session Interface, means the object is now having the relation with the database
3. If we want to convert the object from Transient state to Persistentstate we can do in 2 ways:
1. By saving that object like above
2. By loading object from database
If we do any modifications all the changes will first applied to the object in session cache only (e.g. If we do the modifications 5 times, then 5 times we need to save the changes into the database, which means number of round trips from our application to database will be increased. Actually, if we load an object from the database, first it will be saved in the cache-memory. So, if we do any number of changes all will be effected at cache level only and finally we can call save or update method with a single call such that the data will be saved into the database.
If we want to save an object into database then we need to call any one of the following three methods
(1) save()
(2) persist()
(3) saveOrUpdate()
If we want to load an object from database, then we need to call either load() or get() method(s).
Transient:
One newly created object,with out having any relation with the database, means never persistent, not associated with any Session object.
Persistent:
It has the relation with the database, associated with a unique Session object.
It is also called context stage. It is predefinely created.
It is an abstract class to hide some method.
All the objects which are available for connection with database must be created and loaded and should be made active before data is coming.
All the data should be sent in persistent state first and then in database.
Detached:
Previously having relation with the database [persistent], now not associated with any Session.
Stages:
Stage 1:• Receives the value.
• Makes public data into private.
• Makes received data light weight.
• Makes received data encapsulated.
Stage 2:
Creates Persistent stage or context stage
• Driver
• Connection
• User Name
• Password
• Dilect
Stage 3:
Transient stage
• Sends data in persistent stage
• Makes tablespace in persistent stage.