Hibernate Getting Started 24 - Delayed Initialization

xiaoxiao2021-03-06  45

Getting Started 24 - Delayed Initiation Lazy Initialization

First let's take a look at this topic: Getting started 11 - SET Map The example is completed by this topic, set the Hibernate's show_sql to true, when we use the following program, view the SQL used by the console:

Hibernatetest.java

Import Onlyfun.caterpillar. *;

Import net.sf.hibernate. *;

Import net.sf.hibs. *;

Import java.util. *;

Public class hibernatetest {

Public static void main (string [] args) throws hibernateException {

SESSIONFACTORY sessionFactory = new configuration (). CONFIGURE (). BuildSessionFactory ();

Session session = sessionFactory.openSession ();

List users = session.find ("from user");

session.close ();

SessionFactory.Close ();

}

}

The example of SQL operation is as follows:

Hibernate: SELECT User0_.user_id as user_id, user0_.name as name from user USER0_

Hibernate: select address_.user_id as user_id___, addrs0_.address as address__ from address_.user_id =?

Hibernate: select address_.user_id as user_id___, addrs0_.address as address__ from address_.user_id =?

It can be seen that in addition to reading data from the USER table, read data, Hibernate, reads and loads data through a series of SQL statements, and loading data through a series of SQL statements, however Now consider a situation, we just have to get the name of all USERs, instead of obtaining their mail addresses, the object that is automatically read is unnecessary. In Hibernate, the image of the collective class can delay the initial (when the data is truly requested, in order to query the database, in this example, we don't get it when we read the USER. Object data in the AddRS property, since only needs to read the user's Name property, then we only need to perform the select, you really need the data data of AddRS to request the database. To use Hibernate's delay initial feature, as long as you add lazy = "true",, for example, in in our user.hbm.xml:

User.hbm.xml

Let's take a look at the following procedure:

Hibernatetest.java

Import Onlyfun.caterpillar. *;

Import net.sf.hibernate. *;

Import net.sf.hibs. *;

Import java.util. *;

Public class hibernatetest {

Public static void main (string [] args) throws hibernateException {

SESSIONFACTORY sessionFactory = new configuration (). CONFIGURE (). BuildSessionFactory ();

Session session = sessionFactory.openSession ();

List users = session.find ("from user");

For (listiterator itrator = users.listiterator (); item.hasnext ();) {

User User = (user) iterator.next ();

System.out.println (user.getname ());

Object [] addrs = user.getaddrs (). Toarray ();

For (int i = 0; i

System.out.println ("/ taddress" (i 1) ":" AddRS [i]);

}

}

session.close ();

SessionFactory.Close ();

}

}

At the beginning of the latency, the console will display the following content:

Hibernate: SELECT User0_.user_id as user_id, user0_.name as name from user USER0_

Hibernate: select address_.user_id as user_id___, addrs0_.address as address__ from address_.user_id =?

Hibernate: select address_.user_id as user_id___, addrs0_.address as address__ from address_.user_id =?

Caterpillar

Address 1: Caterpillar@caterpillar.onlyfun.net

Address 2: Justin@caterpillar.onlyfun.net

Address 3: Justin@fake.com

MoMor

Address 1: momor@fake.com

Address 2: momor@caterpillar.onlyfun.net

If you use a delay, the following content occurs:

Hibernate: SELECT User0_.user_id as user_id, user0_.name as name from user USER0_

Caterpillar

Hibernate: select addrs0_.user_id as user_id___, addrs0_.address as address__ from address_.user_id =? Address 1: Caterpillar@caterpillar.onlyfun.net

Address 2: Justin@caterpillar.onlyfun.net

Address 3: Justin@fake.com

MoMor

Hibernate: select address_.user_id as user_id___, addrs0_.address as address__ from address_.user_id =?

Address 1: momor@fake.com

Address 2: momor@caterpillar.onlyfun.net

Note that the location of the SQL statement appears. Before using the delay initial function, all associated data will be checked once, and after using the latency, only the SQL query related data will only be used when the data truly requested addRS. . Hibernate implements the method of delaying the initial function, is to achieve a proxy object by achieving a proxy object (with set, the agent subclass of the agent is Net.SF.Hibernate.Collection.Set), this agent class implements the object of its agent. Related methods, the implementation of each method is actually a delegate true object. Loading a proxy object when queries, before the correction method of true call objects, does not go to the initial real object to perform a call. So in order to use the delay initial, you must use the collection image, you must be the interface of the collection class during the declaration, not a specific implementation class (for example, using SET, instead of Hashset). One problem with the latency is that due to the query database, the session cannot be turned off. If it is closed after session, it will take LazyInitializationException, like:

SET AddRS = User.GetadDRS ();

session.close ();

// The following sentence will happen to LazyInitializationException

Object [] addrs = user.getaddrs (). Toarray ();

If you use a delay initial, while others still need to get related objects after Session shutdown, you can use hibernate.initialize () to load related objects, for example:

Hibernate.initialize (user.getaddrs ());

session.close ();

Set add = user.getaddrs ();

Object [] addo = user.getaddrs (). Toarray ();

Delay initial only Hibernate is a policy when it acquires data. The purpose is to adjust the timing of data inventory to achieve some effectiveness. In addition to delaying, there are other strategies to adjust the method and timing of the information inventory. The discussion involved in some is very large, interested, you can refer to Hibernate In Action 4.4.5.

转载请注明原文地址:https://www.9cbs.com/read-63521.html

New Post(0)