Actually our POJO class object having 3 states like…
- Transient state
- Persistent state
- Detached state
Transient & Persistent states:
- When ever 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 mean not associated with any Session object, if we speak more we can say no relation with the database its just an normal 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 represent 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
Example____ ClientProgram.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
| import org.hibernate.*; import org.hibernate.cfg.*; public class ClientProgram { public static void main(String[] args) { Configuration cfg = new Configuration(); cfg.configure( "hibernate.cfg.xml" ); SessionFactory factory = cfg.buildSessionFactory(); Session session = factory.openSession(); // Transient state_____start Product p= new Product(); p.setProductId( 101 ); p.setProName( "iPhone" ); p.setPrice( 25000 ); // Transient state_____end // Persistent state_____start Transaction tx = session.beginTransaction(); session.save(p); System.out.println( "Object saved successfully.....!!" ); tx.commit(); // Persistent state_____end session.close(); factory.close(); } } |
- see the above client program, line numbers 16 to 19 we just loaded the object and called the corresponding setter methods, its not related to the database row
- 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
- if we want to convert the object from Transient state to Persistentstate we can do in 2 ways
- By saving that object like above
- By loading object from database
If we want to save an object into database then we need to call any one of the following 3 methods
- save()
- persist()
- saveOrUpdate()
If we want to load an object from database, then we need to call either load() or get() methods
Transient:
One newly created object,with out having any relation with the database, means never persistent, not associated with any Session objectPersistent:
Having the relation with the database, associated with a unique Session objectDetached:
previously having relation with the database [persistent ], now not associated with any Sessionsee the next sessions for the better understanding of the life cycle states of pojo class object(s) the hibernate
No comments:
Post a Comment