Hibernate lazy Load Lazy

Source: Internet
Author: User

Lazy loading (lazy load), also known as lazily loading, is a mechanism for delaying loading to avoid the overhead of some unnecessary performance, so-called lazy loading is when the data is really needed to actually perform the data load operation

How to configure delay loading?

In hibernate by assigning values to. HBM's lazy properties, the role of lazy in different locations and the value is different, let's describe separately

A. The lazy in the class tag

We can see that the lazy in the class tag has two values, true and false, and when the lazy value is the default value true, it is lazy loaded and is loaded immediately when the lazy value is false

Let's do an example experiment.

/* * Department entity Class */public class Dept {    //Don't ask the label    private Integer DeptNo;    Private String deptname;    Associate a collection of employees    private set<emp> sets=new hashset<emp> ();    Public set<emp> getsets () {        return sets;    }    public void Setsets (set<emp> sets) {        this.sets = sets;    }    Public Integer Getdeptno () {        return deptNo;    }    public void Setdeptno (Integer deptNo) {        this.deptno = deptNo;    }    Public String Getdeptname () {        return deptname;    }    public void Setdeptname (String deptname) {        this.deptname = deptname;    }    }
<!--dept.hbm.xml-->
<?xml version= "1.0"?> <! DOCTYPE hibernate-mapping Public "-//hibernate /hibernate Mapping DTD 3.0//en "" Http://www.hibernate.org/dt D/hibernate-mapping-3.0.dtd ">
Test-side code @test public    void Totest ()    {            Session session = Hibernateutil.getsession ();        Transaction tx = Session.begintransaction ();        Dept dept= (Dept) session.load (Dept.class, 1);        System.out.println (Dept.getdeptno ());        System.out.println ("================================");                Tx.commit ();        Hibernateutil.closesession ();    }

Note here that the method of Session.get () does not support lazy loading he ignores class-level lazily properties, and when lazy is true, the result is as follows

Then, when you change the lazy to false, the other code is the same, and the result is as follows

We can see the difference between the two values, when the value of the Lazy property is true, Hibernate does not load all the property values one time, only when the program needs to be loaded to reduce the burden on the database interaction and improve the performance of the program, which is the purpose of lazy loading!

Two. The Lazy property in the Set element

If a collection of other entities exists in an object, the set element needs to be configured in the HBM file for mapping between tables, and lazy strong delay loading can also be added to the set element.

The set element has three values for lazy, false (non-inertia loading), true (delayed loading), extra (enhanced delay loading)

Also, we use the above example to test

/* * Department entity Class */public class Dept {    //Don't ask the label    private Integer DeptNo;    Private String deptname;    Associate a collection of employees    private set<emp> sets=new hashset<emp> ();    Public set<emp> getsets () {        return sets;    }    public void Setsets (set<emp> sets) {        this.sets = sets;    }    Public Integer Getdeptno () {        return deptNo;    }    public void Setdeptno (Integer deptNo) {        this.deptno = deptNo;    }    Public String Getdeptname () {        return deptname;    }    public void Setdeptname (String deptname) {        this.deptname = deptname;    }    }
<?xml version= "1.0"?> <! DOCTYPE hibernate-mapping Public "-//hibernate /hibernate Mapping DTD 3.0//en "" Http://www.hibernate.org/dt D/hibernate-mapping-3.0.dtd "> 

Here we only compare the difference between true and extra

@Test public    void Totest ()    {            Session session = Hibernateutil.getsession ();        Transaction tx = Session.begintransaction ();        Dept dept= (Dept) session.load (Dept.class, 1);        System.out.println (Dept.getsets (). Size ());        System.out.println ("================================");        For (Emp item:dept.getSets ()) {            System.out.println (Item.getempname ());        }        Tx.commit ();        Hibernateutil.closesession ();    }

When lazy is true, the run result is as follows

When lazy is extra, the result of the operation is as follows

Hibernate lazy Load Lazy

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.