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