Hibernate Learning Notes-lazy loading lazy-true

Source: Internet
Author: User

1. Lazy loading overview and usage scenarios

Description: lazy loading (lazy), simply called delay, lazy loading.
scenario: in Hibernate framework application, when we want to access the volume of data is too large, the use of cache is not very appropriate, because the memory capacity is limited, in order to reduce the consumption of system resources, reduce concurrency, then need to use lazy loading mechanism to compensate for this flaw, But this does not mean that the overall performance is improved with lazy loading.
Application:
such as school school and student student, school and students 1-to-many, if the lazy set to false, then as long as the load of a school information, will be based on a one-to-many configuration of the relationship between all students to load the information. But in fact sometimes just need to use the school information, do not need to use the student's information, then the load of student information is tantamount to wasting resources. If lazy is set to true, the information about the student's information is only loaded when you access the student information for the school information.

2. The principle of lazy loading
When using the session in Hibernate to query, there are get () and load () Two methods, first say the difference between the two:
The difference between the two methods is that load () has lazy loading characteristics. The Load () method does not directly return the data in the form of a specified object when querying a certain piece of data, but only accesses the database and obtains the data when you really need to use some of the properties of the object. His advantage is that the process itself can be reduced because of the slow processing caused by frequent interactions with the database.
* Session.get ()
* 1, the method is loaded out of the object is a class object
* 2. Issuing SQL statements when the Session.get method executes
* 3, Class object is value
* Session.load ()
* 1, the method is loaded out of the object is the proxy object of class
* 2. Issue SQL statements when loading their properties (load on demand, lazy load)
* 3, the get identifier (UUID) property is not deferred loading, get the normal property is the need to emit the SQL statement
Take the user class as an example

 public  static  void  query  (int  ID) {Session Session=null ; try  {session=hibernateutil.getsession (); User user= (user) Session.load (user.class, id); //system.out.println (User.getname ());  System. out . println (User.getclass ()); }catch  (Hibernateexceptionex) {ex.printstacktrace (); }finally  {if  (Session!=null ) {session.close (); } } }

After running the above method, we do not see Hibernate print any query statement, when the comment statement opens, you can query to the user's name. At this point we can see the query statement produced by Hibernate and see the user's Name property. This is lazy loading.

3. Lazy Load Proxy Object

By printing the User.getclass () method to verify that the printed result is not NULL, is actually a proxy object, and this object belongs to the class is a subclass of the user class, is a subclass of Hibernate automatic implementation.
What is the life cycle of the proxy object?

 public  static  User Span class= "Hljs-title" >query (int  ID) {Session session=null ; User user=null ; try  {session=hibernateutil.getsession (); User= (User) session.load (User.class, id); //system.out.println (User.getname ());  }catch  (Hibernateexceptionex) {ex.printstacktrace (); }finally  {if  (Session!=null ) {session.close (); }} return  User; }

Throws a Org.hibernate.LazyInitializationException exception
This means that if you want to query the database through a proxy object when lazy loading, it needs to be before the session is closed. However, if you must use the proxy object after the session is closed, hibernate defines a method initialize () that initializes the proxy object, which initializes the proxy object.

4. Summary of lazy load function implementation
1. Lazy loading via session.load ()

Load (Object, Serializable): Query by ID. The query returns a proxy object that does not immediately access the database and is lazy-loaded. The database is accessed only when the object is actually being used.
When you use Load (), you will find that the query statement is not printed, and a query statement is printed when you use Get ().
If you use Load () to query this object after the session is closed, the exception is reported: Could not initialize proxy-no Session. Workaround: Initialize the queried object before the session closes: hibernate.initialize (user);

Using load () can improve efficiency because the database was not queried at first. But rarely used.

The 2.one-to-one (element) implements lazy loading.

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 is lazy loading by default. 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.

The 3.many-to-one (element) implements 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.

hibernate3.0 in lazy has three values, True,false,proxy, the default is lazy= "proxy". What is set to look at your needs is not to say which setting is the best. On the label: When True, there will be lazy loading characteristics, when false will produce n+1 problems, such as a student corresponding to a class, with a SQL to find out 10 students, when accessing the student's Class attribute hibernate will generate 10 SQL respectively to identify each student corresponding class.

Lazy= when to take it?

Fetch= Capture Method: Select= Association query; join= Connection Table query (high efficiency)

Fetch=join, lazy settings will have no meaning.
4. One-to-many (Element) Lazy loading: Lazy loading by default, which is required, is heavily used.

One-to-many times, the default is lazy loading when querying the main object. That is, when querying the main object, it is not queried from the object.

A one-to-many time, the query from the object when the default is lazy loading. That is, the query does not query the main object when it is from the object.

You need to configure the set collection in the Master object Lazy= "false" so that it is not lazy to load. Or configure the crawl mode fetch= "Join" can also become non-lazy loading.

Hibernate Learning Notes-lazy loading lazy-true

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.