first, the entity object in Hibernate is divided into three states: transient, persistent, off-tube
transient (transient): This state of the entity object, the database does not have the corresponding data, more than the scope will be reclaimed by the JVM garbage collector, is usually new and has nothing to do with the session object.
Persistent (persistent): There is data in the database corresponding to it, currently associated with the session, and the associated session is not closed, the transaction is not committed. PS: When a persistent object changes, it affects the database when the transaction is committed.
de-detached: There is data in the database corresponding to it, but there is no session associated with it; if the off-tube object changes, hibernate is not detected and does not affect the database.
Add.java:
Package Cn.itcast.hibernate;import Java.util.date;import Org.hibernate.session;import org.hibernate.Transaction; Import Cn.itcast.hibernate.domain.user;public class Add {public static void main (string[] args) {Session s = hibernateutil . GetSession (); Transaction tx = S.begintransaction (); User user = new user (), User.setname ("Zhangsan"), User.setbirthday (New Date ());//The user object at this time is transient, and there is no session associated with it, And it is not mapped to the database S.save (user); At this point the user object is associated with the session, and the user object becomes persistent. System.out.println (User.getname ());//The value of the property name of the user object is output here: Zhangsan. User.setname ("Lisi");//Change the properties of the user object in this case name has the value: Lisi. At this point the user object is still persistent, changing the properties of the user object will affect the database Tx.commit ();//After committing a transaction, the user object becomes a de-System.out.println (User.getname ());// The value of the property name for the output user object here is: Lisi. Moreover, the view database can be seen, inside the Name field value is: Lisi. proves that changing the value of a property can be updated to the database when the user object is persisted. User.setname ("Wangwu"); System.out.println (User.getname ());//The value of the property name for the output user object here is: Wangwu. However, the Name field value in the database is: Lisi. proves that the object is in the off-pipe state at this time. S.close ();}}
second, get, load, persist three methods of detailed: session: is a core interface of Hibernate operations database. A connection is packaged in the session.
1, get (Class,id) method detailed
The session interface has a Get (class User.class,int ID) method that can be used to get a record (object) from the primary key ID
For example, the following code:
Class userclass = User.class;
User user = (user) session.get (userclass,id);
2, Load (Class,id) method Detailed:
There is also a load (Class,id) method in the session interface, which is the same as the parameters of the Get method, except that it does not immediately access the database.
Only the first time you need to use the database, the bottom layer will output the SQL statement. Typically used for lazy loading.
For example:
Session s = null;try{s = Hibernateutil.getsession (); Class userclass = User.class;//user user = (user) S.get (userclass, id); User user = (user) S.load (userclass, id);//load value cannot be empty return user;} Finally{if (s!=null) {s.close ();}}
This code will report an exception. Org.hibernate.LazyInitializationException:could not initialize Proxy-no Session.
Because the database is not used at this time.
If on the top user user = (user) s.load (userclass,id), this line of code is added below:
System.out.println (User.getname ()); The exception is not reported at this time because the database is accessed.
3, the Persist () method detailed:
The persist () method in the session interface is the same as the Save () method. There is a difference when you do not open a transaction:
Save (): When the transaction is not turned on, the method inserts the record into the database and then rolls back. Finally, there is still no record in the database.
Persist (): When a transaction is not opened, the method does not insert the record into the database at all. This method is less used.
"Three big SSH framework" Hibernate basics Third: three States of entity objects and an explanation of the three methods of get, load, persist