Comparison of session get and Load methods

Source: Internet
Author: User

The get test code is as follows:

 Public classTest { Public Static voidMain (string[] args) {//TODO auto-generated Method StubApplicationContext AC=NewClasspathxmlapplicationcontext ("Applicationcontext.xml"); Sessionfactory sessionfactory= (sessionfactory) ac.getbean ("Sessionfactory"); Session Session=sessionfactory.opensession (); User User1= (user) session.get (user).class, 3); System.out.println ("-----------"); System.out.println (user1);    }}

The output is:

Hibernate:
Select
User0_.id as id0_0_,
User0_.name as Name0_0_
From
Mydb.user user0_
where
User0_.id=?
-----------
User [Id=3, Name=sara3]

Load test Code

 Public classTest { Public Static voidMain (string[] args) {//TODO auto-generated Method StubApplicationContext AC=NewClasspathxmlapplicationcontext ("Applicationcontext.xml"); Sessionfactory sessionfactory= (sessionfactory) ac.getbean ("Sessionfactory"); Session Session=sessionfactory.opensession (); User User2= (user) session.load (user).class, 2); System.out.println ("********");        System.out.println (User2);    Session.close (); }}

Output Result:

********
Hibernate:
Select
User0_.id as id0_0_,
User0_.name as Name0_0_
From
Mydb.user user0_
where
User0_.id=?
User [id=2, Name=sara2]

You can see that the GET method emits SQL statements directly from the database, without delay. The load method emits an SQL statement only if the object's contents are actually used

Then use two more test cases

Get test Cases

 Public classTest { Public Static voidMain (string[] args) {//TODO auto-generated Method StubApplicationContext AC=NewClasspathxmlapplicationcontext ("Applicationcontext.xml"); Sessionfactory sessionfactory= (sessionfactory) ac.getbean ("Sessionfactory"); Session Session=sessionfactory.opensession (); User User1= (user) session.get (user).class, 3); System.out.println ("-----------");        System.out.println (User1.getclass ());    Session.close (); }}

The output is:

Hibernate:
Select
User0_.id as id0_0_,
User0_.name as Name0_0_
From
Mydb.user user0_
where
User0_.id=?
-----------
Class Com.db.entity.User

Load test Code:

 Public classTest { Public Static voidMain (string[] args) {//TODO auto-generated Method StubApplicationContext AC=NewClasspathxmlapplicationcontext ("Applicationcontext.xml"); Sessionfactory sessionfactory= (sessionfactory) ac.getbean ("Sessionfactory"); Session Session=sessionfactory.opensession (); User User2= (user) session.load (user).class, 2); System.out.println ("********");        System.out.println (User2.getclass ());    Session.close (); }}

The output is:

********
Class Com.db.entity.user_$$_javassist_0

Conclusion:

  The Get method returns the persisted object immediately after the call to the database, regardless of the cache, and the Load method returns a proxy object that only holds the ID of the entity object until the SQL statement is issued when the object's non-primary key attribute is used

Comparison of session get and Load methods

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.