The definition of lazy loading:
Lazy loading: In a Web application, it is often necessary to query the database, and the response speed of the system is largely the response to the database interaction. Therefore, if you can optimize the speed of interaction with the database, you can greatly improve the response speed of the Web application.
For example, when there is a student class and a teacher class. When we load all the information of a student, including: study number, name and other attributes, at this time the properties of the teacher type in the student class are null, and we need to know the student corresponding teacher attribute, we just load the teacher object.
If we just need to know the student information, we just have to query the Stduent table in the database once and do not need to query the teacher table based on the foreign key
If, we also need to know the teacher information, we query student table, but also need to do a query, query teacher table.
Second, lazy loading principle and analysis:
User.java
public class User {private int id; private String name; private Date birthday;//Setter and getter method omitted}
User.hbm.xml:
<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd ">
This is the entity class and mapping file for user.
Let's write a test class:
public class Base {public static void main (string[] args) {User user = new User ("Lipenglong", New Date ()); AddUser (user); User u = GetUser (User.getid ()); System.out.println ("Birthday:" +u.getbirthday ());} static void AddUser (user user) {configuration cfg = new configuration (); Cfg.configure (); Sessionfactory SF = Cfg.buildsessionfactory (); Session s = sf.opensession (); Transaction tx = s.begintransaction (); S.save (user); Tx.commit (); S.close (); } static User getUser (int id) {Session s = null; User U = null;try{s = Hibernateutil.getsession (); u = (User) s.load (user.class,id);} Finally{if (s!=null) {s.close ();}} return u;}}
As you can see, we first added a new user, and then we load a user object in the getuser (int id) method based on the user ID and then return.The Birthday property of this object is then called in the main function.
But this can cause a mistake:
This is a lazy load exception: When the Session object is opened, we delay loading an object, and then we close the session. This exception is reported when you want to use this object in the main function.
Here are a few solutions:
1. Replace with Get (Class,id) methodWith the Get (Class,id) method, we will generate an SQL statement when the getuser (int id) function executes:
2, after the load (Class,id) method, that is, before the session closes, initialize the object (add: Hibernate.initialize (U); statement).After adding this statement, we can invoke the object's method in the main function:
We can print out the Birthday property.
3, after the load (Class,id) method, that is, before the session closed, the method of invoking this object is equivalent to initialize (add: u.getname (); statement or U.getbirthday ();)static User getUser (int id) {Session s = null; User u = null;try{s = Hibernateutil.getsession (); u = (User) s.load (user.class,id); U.getname ();//u.getbirthday ();} Finally{if (s!=null) {s.close ();}} return u;}
After we have modified it, we can call the method of this object in the main function.
Three, one-to-one lazy loading principle and analysis:On a one-to-one, the default is not lazy loading when querying the main object . That is, when querying the main object, it will also query from the object.
The main object needs to be formulated as lazy= "true" constrained= "true" fetch= "select". When querying the main object, the object is not queried, thus lazy loading is implemented.
On a one-time, the query from the object defaults to lazy loading . That is, the query does not query the main object when it is from the object. Instead, the query is the proxy object of the main object.
Below, we use person and idcard to give examples:
Person.java:
public class Person {private int id;private String name;private idcard idcard;public int getId () {return ID;} public void setId (int id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public Idcard Getidcard () {return idcard;} public void Setidcard (Idcard idcard) {this.idcard = Idcard;}}
Person.hbm.xml:<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd ">
Idcard.java:public class Idcard {private int id;private Date userfullift;private person person;public int getId () {return ID;} public void setId (int id) {this.id = ID;} Public Date Getuserfullift () {return userfullift;} public void Setuserfullift (Date userfullift) {this.userfullift = Userfullift;} Public Person Getperson () {return person;} public void Setperson (person person) {This.person = person;}}
IdCard.hbm.xml:
<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd ">
Then, we write a test class:Package Cn.itcast.hibernate;import Java.util.date;import Org.hibernate.hibernate;import org.hibernate.Session; Import Org.hibernate.transaction;import Cn.itcast.hibernate.domain.idcard;import Cn.itcast.hibernate.domain.person;public class One2One {public static void main (string[] args) {add (); System.out.println ("——————————————————"); Idcard Idcard = Queryidcard (1); System.out.println ("————————————————————"); Person p = Queryperson (1);} Static person Add () {Session s = null; Transaction tx = Null;try{s = Hibernateutil.getsession (); Idcard idcard = new Idcard (); Idcard.setuserfullift (new Date ()); Person p = new person ();p. SetName ("P1");p. Setidcard (Idcard); Idcard.setperson (P); TX =s.begintransaction (); S.save (P); S.save (Idcard); Tx.commit (); return p;} Finally{if (s!=null) {s.close ();}}} static Idcard queryidcard (int id) {Session s = null; Transaction tx = Null;try{s = Hibernateutil.getsession (), TX = S.begintransaction (), Idcard Idcard = (idcard) s.get (IdCard. class, ID); Tx.commit (); return idcard;} Finally{if (s!=null) {s.close ();}}} static person Queryperson (int id) {Session s = null; Transaction tx = Null;try{s = Hibernateutil.getsession (); tx = S.begintransaction (); Person P = (person) s.get (Person.class, id); Tx.commit (); return p;} Finally{if (s!=null) {s.close ();}}}}
In the main function, we call two methods, the query from the object Idcard and query the main object person, we can see the console print out the SQL statement:As you can see, we have generated two INSERT statements and two query statements. The first query statement is simply a query from the Table Id_card table, but the second query statement will be based on a connection to first query the main table person and then query from table Id_card.
So, in a one-to-one association, hibernate defaults from the table for lazy loading, but the primary table does not.
Four, many pairs of an associated lazy loading:When querying the main object, the default is lazy loading. That is, when querying the main object, it is not queried from the object.
When querying from an object, the default is lazy loading. That is, the query does not query the main object when it is from the object.
Five, many-to-many associated lazy loading:
As mentioned above, there will always be lazy loading phenomenon.
Lazy loading is the default in many places in hibernate, so how do we turn off lazy loading?
Take a look at the next article:
"SSH Three Frame" Hibernate Foundation 13th:lazy, constrained, fetch three properties and how to use
"SSH Three frames" Hibernate Foundation 12th: Load () lazy load analysis and a pair of one or one-to-many, many-to-one, many-to-many lazy loading analysis