In the associated mapping file <class> In the tag lazy( Lazy loading ) attribute

Lazy( Lazy loading ): Only if the object is actually being used , To create this object

Hibernate Medium lazy( Lazy loading ): Only when we're really using it , It will send out SQL sentence , Let's go and find out , If you don't use objects, you don't send SQL Statement to query .

Hibernate in lazy( Lazy loading ) The implementation of the :

Libraries using third-party components , This library is called cglib.jar( More popular ), This library generates proxy classes for our classes (JDK Dynamic proxy for , Only right JDK The class that implements the interface in ), The proxy can control the source object and enhance the function of the source object , and cglib.jar You can delegate classes (cglib Inherit from our class , Generate a subclass , This subclass is returned to you as a proxy class ).

You are the only real proxy class , You will see if you have loaded the target object , If not, the target object is loaded .

Lazy( Lazy loading ) stay hibernate Where to use :

1、<class> On the label , It can take :true/false,( The default value is :true)

2、<property> On the label , It can take :true/false, Need class enhancement tools

3、<set>、<list> On the set , It can take :true/false/extra,( The default value is :true)

4、<one-to-one>、<many-to-one> Single ended connection , It can take :false/proxy/noproxy

Session.load() Method support lazy, and session.get() I won't support it lazy;

Hibernate Of lazy Effective period :

Effective date and session Same ,session close ,lazy invalid

hibernate Support lazy The strategy is only in session It works when it's on .

<class> On the label , It can take :true/false,( The default value is :true):

Instance of a : Set up <class On the label lazy=true( Default )

@Test
public void LazyTest() {
session = HibernateUtil.getSession();
tx = session.beginTransaction(); // The execution of this statement does not result in SQL sentence , Just returns a proxy class
Group group = (Group) session.load(Group.class, 1); // I can't SQL sentence , Use the 1 value
System.out.println("group.id=" + group.getId()); // The object is used here , Will be issued SQL sentence
System.out.println("group.name=" + group.getName()); // Commit transaction
tx.commit();
}

Output :

group.id=1
Hibernate: select group0_.id as id1_1_0_, group0_.name as name2_1_0_ from t_group group0_ where group0_.id=?

Set up <class On the label lazy=true( Default )

@Test
public void LazyTest1(){
Session session = null;
Transaction tx = null;
Group group = null; try {
session = HibernateUtil.getSession();
tx = session.beginTransaction(); group =(Group)session.load(Group.class, 1); System.out.println("group.name=" + group.getName()); // Commit transaction
tx.commit();
} catch (Exception e) {
e.printStackTrace();
tx.rollback();
} finally {
HibernateUtil.closeSession(session);
} // Can't output correctly , Throw out LazyInitalizationException abnormal , because session It's closed
//hibernate Support lazy The strategy is only in session It works when it's on .
System.out.println("group.name=" + group.getName());
}

In the set label of the associated mapping file lazy( Lazy loading ) attribute

<set>、<list> On the set , It can take :true/false/extra,( The default value is :true)

Instance of a :( On the assembly lazy=true( Default ))

@Test
public void LazyTest2() { session = HibernateUtil.getSession();
tx = session.beginTransaction();
// It's not going to send out SQL sentence
Classes classes = (Classes) session.load(Classes.class, 1);
// issue SQL sentence , Because you're using objects
System.out.println("classes.name=" + classes.getName()); // I can't SQL sentence , Only one proxy class will be returned , Because no objects are used
Set<Student> students = classes.getStudents(); // Will be issued SQL sentence , Because of the use of objects
for (Iterator<Student> iter = students.iterator(); iter.hasNext();) {
Student student = iter.next();
System.out.println(student.getName());
} // Commit transaction
tx.commit();
}

Example 2 : On the assembly lazy=true( Default )

session = HibernateUtils.getSession();
tx = session.beginTransaction(); // It's not going to send out SQL sentence
Classes classes = (Classes)session.load(Classes.class, 1);
// issue SQL sentence , Because you're using objects
System.out.println("classes.name=" + classes.getName()); // I can't SQL sentence , Only one proxy class will be returned , Because no objects are used
Set<Student> students = classes.getStudents(); // Will be issued SQL sentence , Send out a query for all data SQL, The efficiency is not high
System.out.println("student.count=" + students.size()); // Commit transaction
tx.commit();

Examples of three : On the assembly lazy=false, Others remain default

// It's not going to send out SQL sentence , Because only... On the set is set lazy by false, Others remain default 
Classes classes = (Classes)session.load(Classes.class, 1);
// Send out two SQL sentence , Load separately classes and student
// And load the data in the collection ( Although the objects in the collection are not used ), Because the set of lazy=false
System.out.println("classes.name=" + classes.getName()); // I can't SQL sentence , Because the data has been loaded before
Set<Student> students = classes.getStudents(); // Will be issued SQL sentence , Because the data has been loaded before
for (Iterator<Student> iter = students.iterator();iter.hasNext();){
Student student = iter.next();
System.out.println(student.getName());
}

Instances of four : On the assembly lazy=false, Others remain default

// It's not going to send out SQL sentence 
Classes classes = (Classes)session.load(Classes.class, 1);
// Send out two SQL sentence , Load separately classes and student
// And load the data in the collection ( Although the objects in the collection are not used ), Because the set of lazy=false
System.out.println("classes.name=" + classes.getName()); // I can't SQL sentence , Because the data has been loaded before
Set<Student> students = classes.getStudents(); // I can't SQL sentence , Because the data has been loaded before
System.out.println("student.count=" + students.size());

Example 5 : On the set lazy=extra, Other defaults

session = HibernateUtils.getSession();
tx = session.beginTransaction(); // It's not going to send out SQL sentence
Classes classes = (Classes)session.load(Classes.class, 1); // Will be issued SQL sentence
System.out.println("classes.name=" + classes.getName()); // It's not going to send out SQL sentence , Only the proxy class is returned
Set<Student> students = classes.getStudents(); // Will be issued SQL sentence
for (Iterator<Student> iter = students.iterator();iter.hasNext();){
Student student = iter.next();
System.out.println(student.getName());
}

Example 6 : On the set lazy=extra, Other defaults

session = HibernateUtils.getSession();
tx = session.beginTransaction(); // It's not going to send out SQL sentence
Classes classes = (Classes)session.load(Classes.class, 1);
// Send out two SQL sentence
System.out.println("classes.name=" + classes.getName()); // It's not going to send out SQL sentence
Set<Student> students = classes.getStudents(); // issue SQL sentence , Send out a more intelligent SQL sentence (select count(id) form t_student where classesid=?)
System.out.println("student.count=" + students.size()); // Commit transaction
tx.commit();

<one-to-one>、<many-to-one> On single ended correlation lazy( Lazy loading ) attribute

Ø <one-to-one>、<many-to-one> Single ended connection , It can take :false/proxy/noproxy(false/ agent / Not acting for )

Instance of a : all lazy Property default ( Support lazy loading )

session = HibernateUtils.getSession();
tx = session.beginTransaction(); // Do not send out SQL sentence , Support lazy( Lazy loading )
User user = (User) session.load(User.class, 3);
// issue SQL sentence , Load only normal properties , The data in the collection is not loaded
System.out.println("user.name=" + user.getName()); // It's not going to send out SQL sentence , Only the proxy class is returned
Group group = user.getGroup();
// issue SQL sentence , Because now we really use objects
System.out.println("group.name=" + group.getName());
tx.commit();

Example 2 : take <many-to-one> Medium lazy Set to false, Other defaults

session = HibernateUtils.getSession();
tx = session.beginTransaction(); // It's not going to send out SQL
User user = (User) session.load(User.class, 3);
// Will be issued SQL, Send out two SQL, Namely User And groups
// because <many-to-one> Medium lazy=false, Will load Group
System.out.println("user.name=" + user.getName()); // It's not going to send out , Data has been loaded on it
Group group = user.getGroup();
// It's not going to send out , Data has been loaded on it
System.out.println("group.name=" + group.getName());
tx.commit();

Examples of three : take <class> Medium lazy Set to false, Other defaults

session = HibernateUtils.getSession();
tx = session.beginTransaction(); // Will be issued SQL, because <class> Medium lazy=false
User user = (User) session.load(User.class, 3);
// It's not going to send out SQL, It's already loaded on it
System.out.println("user.name=" + user.getName()); // It's not going to send out , because <class> On the label lazy It only affects ordinary attributes
//<class> On the label lazy It doesn't affect the single ended Association lazy characteristic
Group group = user.getGroup();
// Will be issued , Because you start using objects
System.out.println("group.name=" + group.getName()); tx.commit();

hibernate--lazy( Lazy loading ) More articles on attributes

  1. 018 In the associated mapping file &lt;class&gt; In the tag lazy( Lazy loading ) attribute

    Lazy( Lazy loading ): Only if the object is actually being used , To create this object Hibernate Medium lazy( Lazy loading ): Only when we're really using it , It will send out SQL sentence , Let's go and find out , If you don't use objects, you don't send SQL The sentence goes into ...

  2. 020 &lt;one-to-one&gt;、&lt;many-to-one&gt; On single ended correlation lazy( Lazy loading ) attribute

    <one-to-one>.<many-to-one> Single ended connection , It can take :false/proxy/noproxy(false/ agent / Not acting for ) Instance of a : all lazy Property default ( Support ...

  3. 019 In the set label of the associated mapping file lazy( Lazy loading ) attribute

    <set>.<list> On the set , It can take :true/false/extra,( The default value is :true) Instance of a :( On the assembly lazy=true( Default ))class Default lazy=tru ...

  4. hibernate Lazy loading problem of

    The reasons causing : When using hibernate When querying an object , If Session close , When calling the collection or object associated with the object , A lazy load exception is generated ! Solution : Scheme 1 : stay Session Before closing , Query objects associated with ...

  5. Hibernate Lazy loading session Lost solutions

    stay web.xml Join in spring Filter provided , extend session Life cycle of <!--Hibernate Lazy loading session Lost solutions --> <filter> <fi ...

  6. hibernate in fetch=FetchType.LAZY Lazy load failure handling

    For this lazy loading problem , The last thing to do is to use Spring Provide a target for Hibernate A support class for , Its main meaning is to open when a page request is initiated Hibernate Of Session, Keep this Session, bring Hi ...

  7. hibernate Lazy loading

    WHY? WHAT? HOW? Lazy loading (lazy) Delay loading , Delay loading . It's not that it doesn't load , It's loaded when it's needed . When to use lazy loading , I can only answer that if you want to use lazy loading, use lazy loading . Why use lazy loading ...

  8. Hibernate @OneToOne Lazy loading implementation solution

    stay hibernate annotation ( 3、 ... and ) in , I improved one-on-one (@OneToOne) The problem of lazy loading failure . Although the solution is given , But there is no complete solution . Today, I will focus on this issue . As for the cause of lazy loading failure , Previous articles ...

  9. hibernate+spring mvc, solve hibernate Object lazy loading json Serialization problem

    Refer to the address In the use of Spring MVC when ,@ResponseBody An exception occurred when the annotation method returned a lazy loading object , Take logging, for example : @RequestMapping("login") ...

Random recommendation

  1. SQL Server How to add a login

    1. If you have SQL Server, We open it in the beginning SQL Server Management Studio, Or open it with desktop shortcut, etc SQL Server. 2. First of all Windows Authentication ...

  2. bootstrap Plug in learning -bootstrap.button.js

    First look at bootstrap.button.js Structure var Button = function ( element, options ){} // Constructors Button.prototype = {} // ...

  3. CSS3 note

    CSS/CSS3 Online manual :http://www.css119.com/book/css/   CSS3 Achieve horizontal and vertical centering :http://bbs.html5cn.org/thread-87300-1-1. ...

  4. stay JBoss AS7 Project deployment in

    http://developer.51cto.com/art/201111/305178.htm

  5. Efficient view MySQL Help document method ( turn )

    stay mysql In the course of using , You may often encounter the following problems : Some operation syntax has been forgotten , How to quickly find ? How to quickly know the value range of a field type in the current version ? Which functions are supported in the current version ? I hope there are examples to illustrate .. Does the current version support ...

  6. SOFA Source code analysis —— Service reference process

    Preface In front of SOFA Source code analysis -- Service release process In the article , We analyzed SOFA The service publishing process of , A complete RPC Besides publishing services , Of course, you also need to reference services . So, Let's have a look today SOFA How to lead ...

  7. C# Finding the mode of a group of numbers

    private int GetModeNum(List<int> listValue) { List<int> listName = new List<int>() ...

  8. A deep understanding Python The metaclass in (metaclass)【 turn 】

    Translation notes : This is an article in Stack overflow Hot posts on the Internet . The questioner claimed to have mastered the knowledge of Python OOP Concepts in programming , But I always feel that metaclasses (metaclass) Difficult to understand . He knew it had to do with introspection , But still feel ...

  9. RxJS The transformation operator ( Angular Environmental Science )

    One map The operator It's similar to what we all know  Array.prototype.map  Method , This operator applies the projection function to each value And output Observable The result of the projection in . import { Compo ...

  10. QR code Data Matrix code 、 Decoding uses examples

    QR code Data Matrix See : http://blog.csdn.net/fengbingchun/article/details/44279967  , Here is a simple write to generate two-dimensional code and two-dimensional code for ...