Hibernate 4. Delay loading

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.