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);
}