Delay Loading:
The query statement is sent to the database only if the attribute (except the primary key attribute) is used with the entity object
Get does not support deferred loading
@Testpublic void Gettest () {Session session=null; Student student=null;try{session=hibernateutil.opensession (); student= (Student) Session.get (Student.class, 3); SYSTEM.OUT.PRINTLN ("ID:" +student.getstudentid ()); System.out.println ("Name:" +student.getstudentname ());} Finally{session.close ();}}
Results:
Hibernate:select student0_.student_id as student1_0_0_, student0_.student_name as student2_0_0_, student0_.age as age0_ 0_ from T_student student0_ where student0_.student_id=?id:3name:ddddd
A query statement has been issued when the Get method is called
Load supports deferred loading
@Testpublic void LoadTest () {Session session=null; Student student=null;try{session=hibernateutil.opensession (); student= (Student) session.load (Student.class, 3); SYSTEM.OUT.PRINTLN ("ID:" +student.getstudentid ()); System.out.println ("Name:" +student.getstudentname ());} Finally{session.close ();}}
Results:
Id:3hibernate:select student0_.student_id as student1_0_0_, student0_.student_name as student2_0_0_, student0_.age as age0_0_ from T_student student0_ where student0_.student_id=?name:ddddd
When the load method is called, a proxy object is returned, and no query statement is issued, and the query statement is issued when the Studentname property is required
After closing the session
Get
@Testpublic void Get2test () {Session session=null; Student student=null;try{session=hibernateutil.opensession (); student= (Student) Session.get (Student.class, 3);} Finally{session.close ();} SYSTEM.OUT.PRINTLN ("ID:" +student.getstudentid ()); System.out.println ("Name:" +student.getstudentname ());}
Results:
Hibernate:select student0_.student_id as student1_0_0_, student0_.student_name as student2_0_0_, student0_.age as age0_ 0_ from T_student student0_ where student0_.student_id=?Id:3name:ddddd
Same as the result in the session scope
Load
@Testpublic void Load2test () {Session session=null; Student student=null;try{session=hibernateutil.opensession (); student= (Student) session.load (Student.class, 3);} Finally{session.close ();} SYSTEM.OUT.PRINTLN ("ID:" +student.getstudentid ()); System.out.println ("Name:" +student.getstudentname ());}
Results:
Id:3org.hibernate.lazyinitializationexception:could not initialize Proxy-no Session
Cast exception, visible delay loading is only valid within the scope of the session
Summary:
1. Only the query statement is sent to the database when the attribute (except the primary key attribute) is used with the entity object
2. Delay loading is only valid within the session's scope
Hibernate 4. Delay loading