The difference between Hibernate list and iterate get and load

Source: Internet
Author: User
Tags commit readable

List and iterate:
1 list is to query iterate immediately using lazy loading, it is possible to generate N+1 queries
Session session=hibernatesessionfactory.getsession ();
System.out.println ("1:" +session);
Session.begintransaction ();
Query query1=session.createquery ("from Books where id=7");
List<books> list=query1.list ();
/*system.out.println ("2:" +session);
for (Books b:list) {
System.out.println (B.getauthor () + "" +b.getname ());
}*/
Query query=session.createquery ("from Books where id<7");
Iterator<books> books=query.iterate ();
System.out.println ("3:" +session);
while (Books.hasnext ()) {
Books Book=books.next ();
Book.getname ();
}
Session.gettransaction (). commit ();
Hibernatesessionfactory.closesession ();
System.out.println ("4:" +session);


2 list is writable and unreadable to level 1 cache
Iterate to 1-level cache writable and readable
Session session=hibernatesessionfactory.getsession ();
System.out.println ("1:" +session);
Session.begintransaction ();
Query query1=session.createquery ("from Books");
Iterator<books> books=query1.iterate ();
System.out.println ("2:" +session);
while (Books.hasnext ()) {
Books Book=books.next ();
Book.getname ();
System.out.println ("3:" +session);
}
/*system.out.println ("2:" +session);
for (Books b:list) {
System.out.println (B.getauthor () + "" +b.getname ());
}*/
Query query=session.createquery ("from Books");
Iterator<books> books1=query.iterate ();
System.out.println ("3:" +session);
while (Books1.hasnext ()) {
Books Book=books1.next ();
Book.getname ();
System.out.println ("3:" +session);
}
Session.gettransaction (). commit ();
Hibernatesessionfactory.closesession ();
System.out.println ("4:" +session);

}

Get vs. Load:
1 get is immediately loaded load is delayed loading, load just returns a proxy object,

This proxy object has only the ID, no other data, and only queries the database when it triggers the must query point
Session session=hibernatesessionfactory.getsession ();
System.out.println ("1:" +session);
Session.begintransaction ();
Books b1= (Books) session.load (Books.class, 2);
B1.getname ();
System.out.println ("2:" +session);
Books b2= (Books) Session.get (Books.class, 3);

System.out.println ("3:" +session);

Session.gettransaction (). commit ();
Hibernatesessionfactory.closesession ();
System.out.println ("4:" +session);


2 Get if you don't get the exact data, then manipulate the object and report a null pointer exception
If the load does not obtain the specific data, the report objectnotfoundexception


Session session=hibernatesessionfactory.getsession ();
 system.out.println ("1:" +session);
 session.begintransaction ();
    Books b1= (Books) session.load (Books.class, 2);
    b1.getname ();
    System.out.println ("2:" +session);
    Books b2= (Books) Session.get (Books.class, 3);
  
    System.out.println ("3:" +session);
   
  Session.gettransaction (). commit ();
 hibernatesessionfactory.closesession ();
  B2.getauthor ();
 system.out.println ("4:" +session);
 

 


3 Get to Level 1 cache readable writable
  load to Level 1 cache readable writable  
Session session=hibernatesessionfactory.getsession ();
& nbsp System.out.println ("1:" +session);
 session.begintransaction ();
    Books b1= (Books) session.load (Books.class, 2);
    b1.getname ();
    System.out.println ("2:" +session);
    Books b2= (Books) Session.get (Books.class, 3);
    System.out.println ("3:" +session);    
 session.gettransaction (). Commit ();
 hibernatesessionfactory.closesession ();
  B2.getauthor ();
 system.out.println ("4:" +session);
 
}

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.