One, what is deferred loading
In order to save the performance of Hibernate-loaded objects, the object is actually used in hibernate to emit
SQL statement to fetch the object. This process is called lazy loading.
Ii. Classification of deferred loading
A: Lazy Loading of entity objects
B: One-to-many | Many-to-many delay loading
C: Many-to-one | single-on delay loading
D: Lazy Loading of attributes
- A: Lazy Loading of entity objects: the difference between getting an object using Session.get () and Session.load () is whether to turn on lazy loading.
Hibernate only loads the ID of the entity object and requires additional attributes to actually emit the SQL to load the object.
Load: Loaded with a deferred load to a proxy object
Get: A solid object is not loaded with lazy loading.
User user= (user) Session.load (clazz, id);//The proxy object is returned directly
System.out.println (User.getid ());//No SQL statement sent to database load
User.getname ();//Create a real user instance and send the SQL statement to the database
- Note: 1. Cannot judge User=null; proxy objects cannot be empty
2. Proxy object Restrictions: The session object associated with the agent, if the session is closed after the access agent throws an exception. Access database before session closes
- B: One-to-many | Many-to-many delay loading
Fetch = Fetchtype.lazy: Indicates that lazy loading is turned on. When reading a class, the SQL statement that reads the student is not emitted. When the student data is actually used, a SQL statement is issued to read the student
Fetch = Fetchtype.eager: Cancels the delay and cuts. Reading the class will read the student to the left association.
@OneToMany (cascade = {Cascadetype.remove},fetch=fetchtype.eager)
@JoinColumn (name = "classes_id")
@OrderBy (value = "StudentID desc")
Public list<studentbean> getstulist () {
return stulist;
}
- C: Many-to-one | The default is to cancel deferred loading.
@ManyToOne (Fetch=fetchtype.lazy)
@JoinColumn (name = "group_id")
Private Groupbean Groupbean;
- Problems with lazy loading: After the session is closed, the Access Proxy object (deferred loading Gets the proxy object) throws a "no session" exception.
PackageAction;ImportJava.util.Set;ImportJavassist.compiler.ast.IntConst;Importorg.hibernate.Session;Importorg.hibernate.Transaction;ImportBean. Classbean;ImportBean. Studentbean;Importutil. Hibernatesessionutil; Public classTest { Public Static voidMain (string[] args) { Classbean cla =test.load (); //when Test.load () is executed, the session is closed, and the proxy object is then thrown with an exception. Use session. Get does not show this problemSystem.out.println (Cla.getclassname ()); } Private StaticClassbean Load () {Classbean CLA=NULL; Session Session=NULL; Transaction Tran=NULL; Try{Session=hibernatesessionutil.getsession (); Tran=session.begintransaction (); CLA= (Classbean) session.load (Classbean.class,NewInteger (2));//using lazy loading, you get a proxy objectTran.commit (); returnCLA; } Catch(Exception e) {e.printstacktrace (); Tran.rollback (); } finally{hibernatesessionutil.closesession (); //Close Session } return NULL; }}
- The "no session" exception appears in the code at the Orange font.
(16) Lazy loading in Hibernate