In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "lazy attribute summary in Hibernate". In daily operation, I believe many people have doubts about lazy attribute summary in Hibernate. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "lazy attribute summary in Hibernate". Next, please follow the editor to study!
The influence of the lazy attribute of hibernate on reading a child collection of a parent class in one-to-many situations is summarized. (the following code runs under the environment of jdk1.5,jboss eclipse ide 1.5 and hibernate 3.1)
Suppose you have the parent class Person (with the Set type attribute Address) and the subclass Address (as it happens that both the name of the collection and the name of the subclass are Address, don't be confused) the main Person.hbm.xml snippet:
< id name="idx" column="idx" type="long"> < generator class="identity"/> < /id> < property name="age" type="int" update="true" insert="true"column="age"/> < property name="name" type="java.lang.String" update="true"insert="true" column="name"/> < set name="address" table="address" lazy="true" cascade="none" sort="unsorted"> < key > < column name="personidx"/> < /key> < one-to-many class="com.abc.common.pojo.Address"/> < /set>During the cycle of session, there will be no restrictions regardless of whether the lazy property of hibernate is set to true or false. The code to access the parent-child data is as follows:
/ / Open session session = HibernateUtil.currentSession (); PersonDAO dao = new PersonDAO (); Person person = null; person = (Person) dao.findByPrimaryKey (4); Set addressSet = person.getAddress (); Address [] addressAry = new Address [addressSet.size ()]; Address address = null; addressSet.toArray (addressAry); for (int iTun0; I < addressAry.length;i++) {. } / / session closes session.close (); if (session.isOpen ()) {HibernateUtil.closeSession ();}
(2) outside the cycle of session, the code for accessing parent-child data is as follows:
/ / Open session session = HibernateUtil.currentSession (); PersonDAO dao = new PersonDAO (); Person person = null; person = (Person) dao.findByPrimaryKey (4); session.close (); / / access the subset of person Set addressSet = person.getAddress () after session is closed; Address [] addressAry = new Address [addressSet.size ()]; Address address = null; addressSet.toArray (addressAry); for (int iTun0; I < addressAry.length) I am +) {. } if (session.isOpen ()) {HibernateUtil.closeSession ();}
At this point, the running result of the above code varies depending on the setting of the lazy property of hibernate
Lazy=false
Result: the data obtained from Person and Address can be accessed
Lazy= true
It varies according to how the code is written.
(1) if no processing is done elsewhere in the code, an exception is thrown
Org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role:
(2) if you do some processing as follows, replace the "for subsequent processing" in the above code with the following code
Hibernate.initialize (person.getAddress ()); the data obtained from Person and Address can be accessed.
When you actually write it, it will not be written like this, that is,
Hibernate.initialize (person.getAddress ()); is called in the same method as person.getAddress (). They often appear in different layers of the application (the former occurs mostly in the DAO layer, while the latter occurs mostly in the web layer).
At this point, the study on "summary of lazy attributes in Hibernate" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.