The difference between hibernate's load and get

Source: Internet
Author: User

Both load and get can play a role in getting persistent data from the database, but there are some slight differences.


Refer to this example below:

@Test (expected = illegalargumentexception.class) public void Testlazyload () throws Exception {//Start session session    = Sessionfactory.opensession ();    Transaction tx = Session.begintransaction ();  User user = (user) session.load (user.class, 100L);    Non-existent ID try {user.getname (); } catch (Objectnotfoundexception ex) {//Hit database found no object is thrown objectnotfoundexception exception throw new Illegalargumente    Xception ("casually throws an impossible exception");    } tx.commit (); Session.close ();} @Test (expected = illegalargumentexception.class) public void Testlazyload () throws Exception {//Start session session    = Sessionfactory.opensession ();    Transaction tx = Session.begintransaction ();  User user = (user) session.load (user.class, 100L);    Non-existent ID assert.asserttrue (user instanceof Hibernateproxy);  User.getid ();  Because the ID is a property that is not loaded lazily, the exception is not thrown by the try {hibernate.initialize (user);    Trigger hit Database//user.getname (); } catch (Objectnotfoundexception ex) {//Hit database found no this pairLike throw objectnotfoundexception exception throw new IllegalArgumentException ("exception");    } tx.commit (); Session.close ();}
1. Here we can see that load does not directly get the data, but rather get to the proxy object-->hibernateproxy, if you do not get the properties of the simple type will not throw an exception, but get is not the same, he went directly to the database to fetch, he got the object itself, If not, a null value is returned.


Let's look at an example:

@Testpublic void Testgetandloadsession () throws Exception {//start session session = Sessionfactory.opensession ();    Transaction tx = Session.begintransaction ();  Whether load will cause the loaded object to be cached in the case of an empty cache User user1 = (user) session.load (user.class, 1L);    The existing ID, although it has not been unlocked, but has entered the cache Assert.asserttrue (User1 instanceof Hibernateproxy);  Hibernate.initialize (user1);    Unlocking the proxy will trigger a hit database operation User User3 = (user) Session.get (user.class, 1L);  Assert.asserttrue (User3 instanceof Hibernateproxy);  Even if you use get, but because the cache is stored in a proxy, so here is the proxy hibernate.initialize (USER3);  Unlock proxy, but not hit database//Verify after load a nonexistent ID, do not unlock then get User user4 = (user) session.load (user.class, 100L);    Does not exist ID, will still enter the proxy into the cache assert.asserttrue (user4 instanceof Hibernateproxy);  Hibernate.initialize (USER3);  Do not unlock proxy try {session.get (User.class, 100L);    Get a proxy, hit the database to try to solve the proxy, because the ID does not exist so throw an exception Assert.fail ("ID does not exist so will error, will not execute this article");  } catch (Objectnotfoundexception ex) {}//Empty cache  Session.clear ();  Verify that the cache is empty if get is proxy User User6 = (user) Session.get (user.class, 1L); Hit the database, directly assemble the completed user entity into the cache assert.asserttrue (!)    User6 instanceof Hibernateproxy));    Verify get Fetch object from cache User User7 = (user) Session.get (user.class, 1L); Assert.asserttrue (! ( User7 instanceof Hibernateproxy));    The cache is a real user object, and get takes out the real user object//verifies that the load is fetching data from the first level cache user User8 = (user) session.load (user.class, 1L); Assert.asserttrue (! (  User8 instanceof Hibernateproxy));    The cache is the real user object, and the load takes out the real user Object Tx.commit (); Session.close ();}

2. If the Load method loads the object into the cache, then both get and load are proxy objects, but if the Get method loads the object into the cache, then the get and load are given an entity object.


The difference between hibernate's load and get

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.