020 <one-to-one> and <pull-to-one> lazy (lazy loading) attributes on single-ended associations

Source: Internet
Author: User

<One-to-one> and <others-to-one> Single-ended associations can be set to false/proxy/noproxy (false/proxy/no proxy)

 

Instance 1: default for all lazy attributes (support for lazy loading)

Session = HibernateUtils.GetSession();

Tx = session. beginTransaction ();

 

// Do not issue SQL statements. lazy (lazy loading) is supported)

User user = (User) session. load (User.Class, 3 );

// Issue an SQL statement to load only common attributes. Data in the set is not loaded.

System.Out. Println ("user. name =" + user. getName ());

           

// No SQL statement is issued and only the proxy class is returned.

Group group = user. getGroup ();

// Issue an SQL statement because the object is actually used

System.Out. Println ("group. name =" + group. getName ());

Tx. commit ();

 

Example 2: Set<Role-to-one>InLazySetFalse, Other default

Session = HibernateUtils.GetSession();

Tx = session. beginTransaction ();

 

// The SQL statement is not sent

User user = (User) session. load (User.Class, 3 );

// An SQL statement is sent and two SQL statements are sent, namely User and group.

// Because lazy = false in <role-to-one>, the Group

System.Out. Println ("user. name =" + user. getName ());

           

// It will not be sent, and data has been loaded on it

Group group = user. getGroup ();

// It will not be sent, and data has been loaded on it

System.Out. Println ("group. name =" + group. getName ());

Tx. commit ();

 

Example 3: Set<Class>InLazySetFalse, Other default

Session = HibernateUtils.GetSession();

Tx = session. beginTransaction ();

 

// The SQL statement is sent because lazy = false in <class>

User user = (User) session. load (User.Class, 3 );

// No SQL statement will be issued and it has been loaded on it

System.Out. Println ("user. name =" + user. getName ());

           

// Will not be issued, because the lazy on the <class> label only has an impact on common attributes.

            // <Class>TagLazyDoes not affectLazyFeatures

Group group = user. getGroup ();

// Will be sent because the object is used

System.Out. Println ("group. name =" + group. getName ());

 

Tx. commit ();

020 <one-to-one> and <pull-to-one> lazy (lazy loading) attributes on single-ended associations

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.