-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Failed To Lazily Initialize, But It Works With Other Set
PostPosted: Wed Jun 06, 2007 1:15 pm 
Beginner
Beginner

Joined: Fri Jun 04, 2004 12:50 pm
Posts: 32
Hi all,

I have a weird problem. I have a User object which has associated with it a SortedSet of type BookToSell which are books the user is making available to sell and a SortedSet of type MailMessage which are all the mail messages that the system has sent on the users behalf to those users searching for a similar book.

I am using the typical web application pattern where a filter creates a session at the beginning of the request and end the session when the request is returned.

I do not have any problems accessing or dealing with the BookToSell set of data. Everything initializes as is to be expected whenever I need to get data from the set. But, when I try to access the MailMessage set it fails with the usual "failed to lazily initialize a collection of role....". Now I know that I could make the set NON-lazy but I really do not want to do this. If I do this for testing purposes things work out fine. But certain functions take longer because of the loading requirements.

Attached are the mapping docs and excerpts of the code. Any help on this would be greatly appreciated.

Hibernate version:
I am using JBoss 4.0.5GA. Not sure what hibernate version that is.

Mapping documents:

USER mapping

<hibernate-mapping>

<class name="com.unibabble.common.user.User" table="tblUser">

<cache usage="read-write"/>

<id name="ID" type="integer">
<column name="id_user" sql-type="integer" not-null="true"/>
<generator class="native">
<param name="sequence">tbluser_id_user_seq</param>
</generator>
</id>

<property name="username">
<column name="username" sql-type="char(128)" not-null="true" unique="true"/>
</property>

<property name="firstName">
<column name="first_name" sql-type="char(64)" not-null="true"/>
</property>

<property name="lastName">
<column name="last_name" sql-type="char(64)" not-null="true"/>
</property>

<property name="password">
<column name="password" sql-type="char(64)" not-null="true"/>
</property>

<property name="confirmed">
<column name="confirmed" sql-type="boolean" not-null="false"/>
</property>

<property name="enabled">
<column name="enabled" sql-type="boolean" not-null="false"/>
</property>

<property name="phoneOne">
<column name="phone_one" sql-type="char(64)" not-null="false"/>
</property>

<property name="phoneTwo">
<column name="phone_two" sql-type="char(64)" not-null="false"/>
</property>

<property name="phoneCell">
<column name="phone_cell" sql-type="char(64)" not-null="false"/>
</property>

<property name="canCall">
<column name="can_call" sql-type="boolean" not-null="true"/>
</property>

<property name="confirmationString">
<column name="confirmation_string" sql-type="char(128)" not-null="true"/>
</property>

<property name="dateEntered" update="false">
<column name="date_entered" sql-type="timestamp" not-null="false"/>
</property>

<property name="revisionDate">
<column name="revision_date" sql-type="timestamp" not-null="false"/>
</property>

<many-to-one name="university" cascade="none" class="com.unibabble.common.university.University" column="fk_id_university" not-null="true"/>

<set name="booksToSell" sort="natural" cascade="all-delete-orphan" inverse="true">
<key column="fk_id_user"/>
<one-to-many class="com.unibabble.common.event.book.BookToSell"/>
</set>

<set name="booksToSearch" sort="natural" cascade="all-delete-orphan" inverse="true">
<key column="fk_id_user"/>
<one-to-many class="com.unibabble.common.event.book.BookToSearch"/>
</set>

<set name="toMailMessages" sort="natural" cascade="all-delete-orphan" inverse="true">
<key column="fk_id_to_user"/>
<one-to-many class="com.unibabble.common.mail.MailMessage"/>
</set>

<set name="fromMailMessages" sort="natural" cascade="all-delete-orphan" inverse="true">
<key column="fk_id_from_user"/>
<one-to-many class="com.unibabble.common.mail.MailMessage"/>
</set>

</class>

</hibernate-mapping>


BOOKTOSELL mapping

<hibernate-mapping>

<class name="com.unibabble.common.event.book.BookToSell" table="tblBookToSell">

<cache usage="read-write"/>

<id name="ID" type="integer">
<column name="id_book_to_sell" sql-type="integer" not-null="true"/>
<generator class="native">
<param name="sequence">tblbooktosell_id_book_to_sell_seq</param>
</generator>
</id>

<property name="estimatedPrice">
<column name="estimated_price" sql-type="big_decimal"/>
</property>

<property name="soldPrice">
<column name="sold_price" sql-type="big_decimal"/>
</property>

<property name="isSold">
<column name="is_sold" sql-type="boolean" not-null="false"/>
</property>

<property name="classCode">
<column name="class_code" sql-type="char(16)"/>
</property>

<property name="bookCondition">
<column name="book_condition" sql-type="char(64)"/>
</property>

<property name="professorName">
<column name="professor_name" sql-type="char(128)"/>
</property>

<property name="dateActivated">
<column name="date_activated" sql-type="timestamp"/>
</property>

<property name="dateDeActivated">
<column name="date_deactivated" sql-type="timestamp"/>
</property>

<property name="activationLength">
<column name="activation_length" sql-type="big_decimal"/>
</property>

<many-to-one name="book" cascade="none" class="com.unibabble.common.book.Book" column="fk_id_book"/>
<many-to-one name="user" cascade="none" class="com.unibabble.common.user.User" column="fk_id_user"/>

</class>

</hibernate-mapping>


MAILMESSAGE mapping
<class name="com.unibabble.common.mail.MailMessage" table="tblMailMessage">

<cache usage="read-write"/>

<id name="ID" type="integer">
<column name="id_mail_message" sql-type="integer" not-null="true"/>
<generator class="native">
<param name="sequence">tblmailmessage_id_mail_message_seq</param>
</generator>
</id>

<property name="toAddresses">
<column name="to_addresses" sql-type="text" not-null="true"/>
</property>

<property name="fromAddress">
<column name="from_address" sql-type="char(128)" not-null="true"/>
</property>

<property name="dateSent">
<column name="date_sent" sql-type="timestamp" not-null="true"/>
</property>

<property name="sent">
<column name="sent" sql-type="boolean" not-null="true"/>
</property>

<property name="fromEventType">
<column name="from_event_type" sql-type="integer" not-null="true"/>
</property>

<property name="toEventType">
<column name="to_event_type" sql-type="integer" not-null="true"/>
</property>

<property name="fromEventID">
<column name="from_event_id" sql-type="integer" not-null="true"/>
</property>

<property name="toEventID">
<column name="to_event_id" sql-type="integer" not-null="true"/>
</property>

<set name="mailMessageParameterInstances" cascade="all-delete-orphan">
<key column="fk_id_mail_message"/>
<one-to-many class="com.unibabble.common.mail.MailMessageParameterInstance"/>
</set>

<many-to-one name="mailMessageTemplate" column="fk_id_mail_message_template" class="com.unibabble.common.mail.MailMessageTemplate" not-null="true"/>

<many-to-one name="fromUser" cascade="none" class="com.unibabble.common.user.User" column="fk_id_from_user"/>
<many-to-one name="toUser" cascade="none" class="com.unibabble.common.user.User" column="fk_id_to_user"/>

</class>

</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():
I can add code if people need it. There is code in many different classes.

Full stack trace of any exception that occurs:
10:19:36,906 ERROR [LazyInitializationException] failed to lazily initialize a collection of role: com.unibabble.common.user.User.toMailMessages, no sessio
n or session was closed
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.unibabble.common.user.User.toMailMessages, no session or s
ession was closed
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358)
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:350)
at org.hibernate.collection.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:97)
at org.hibernate.collection.PersistentSet.size(PersistentSet.java:139)
at com.unibabble.actions.book.SellBookAction.execute(Unknown Source)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:419)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at com.unibabble.common.utilities.HibernateFilter.doFilter(Unknown Source)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:264)
at org.acegisecurity.intercept.web.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:107)
at org.acegisecurity.intercept.web.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:72)
at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:274)
at org.acegisecurity.ui.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:110)
at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:274)
at org.acegisecurity.providers.anonymous.AnonymousProcessingFilter.doFilter(AnonymousProcessingFilter.java:125)
at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:274)
at org.acegisecurity.ui.rememberme.RememberMeProcessingFilter.doFilter(RememberMeProcessingFilter.java:142)
at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:274)
at org.acegisecurity.wrapper.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:81)
at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:274)
at org.acegisecurity.ui.AbstractProcessingFilter.doFilter(AbstractProcessingFilter.java:217)
at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:274)
at org.acegisecurity.ui.logout.LogoutFilter.doFilter(LogoutFilter.java:106)
at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:274)
at org.acegisecurity.context.HttpSessionContextIntegrationFilter.doFilter(HttpSessionContextIntegrationFilter.java:229)
at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:274)
at org.acegisecurity.util.FilterChainProxy.doFilter(FilterChainProxy.java:148)
at org.acegisecurity.util.FilterToBeanProxy.doFilter(FilterToBeanProxy.java:98)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:175)
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:74)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.jboss.web.tomcat.tc5.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:156)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112)
at java.lang.Thread.run(Thread.java:595)
Name and version of the database you are using:
Postgres 8.1

The generated SQL (show_sql=true):
No SQL created

Debug level Hibernate log excerpt:
11:00:35,890 DEBUG [AbstractBatcher] about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
11:00:35,890 DEBUG [ConnectionManager] opening JDBC connection
11:00:35,906 DEBUG [SQL] insert into tblApplicationEvent (event_timestamp, messages, result, fk_id_user, fk_id_application_event_type, id_application_event
) values (?, ?, ?, ?, ?, ?)
11:00:35,906 INFO [STDOUT] Hibernate: insert into tblApplicationEvent (event_timestamp, messages, result, fk_id_user, fk_id_application_event_type, id_app
lication_event) values (?, ?, ?, ?, ?, ?)
11:00:35,906 DEBUG [AbstractBatcher] Executing batch size: 1
11:00:35,921 DEBUG [AbstractBatcher] about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
11:00:35,921 DEBUG [ConnectionManager] skipping aggressive-release due to flush cycle
11:00:35,921 DEBUG [ConnectionManager] aggressively releasing JDBC connection
11:00:35,921 DEBUG [ConnectionManager] releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
11:00:35,937 DEBUG [EntityHelper] Leaving modifyEntity
11:00:35,937 DEBUG [EventHelper] Finished addEvent
11:00:35,937 DEBUG [EntityHelper] Leaving modifyEntity
11:00:35,937 DEBUG [MailHelper] Finished sendMailMessage(message)
11:00:35,937 DEBUG [MailHelper] Finished sendMailMessage(list)
11:00:35,953 DEBUG [BookToSellHelper] Finished sendBuyersMailMessages
11:00:35,953 DEBUG [BookToSellHelper] Finished sendSellMailMessages
11:00:35,953 DEBUG [BookToSellHelper] Finished sellBook
11:00:35,953 ERROR [LazyInitializationException] failed to lazily initialize a collection of role: com.unibabble.common.user.User.toMailMessages, no sessio
n or session was closed
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.unibabble.common.user.User.toMailMessages, no session or s
ession was closed
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358)
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:350)
at org.hibernate.collection.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:97)
at org.hibernate.collection.PersistentSet.size(PersistentSet.java:139)
at com.unibabble.actions.book.SellBookAction.execute(Unknown Source)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:419)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 06, 2007 5:19 pm 
Red Hat Associate
Red Hat Associate

Joined: Mon Aug 16, 2004 11:14 am
Posts: 253
Location: Raleigh, NC
Are you sticking User into HTTPSession? That's typically how this happens. Once the Session that loaded that User is closed, the lazy loading won't work. It doesn't matter that *a* session it open because the entity is now detached from its loading Session.

In other news, I highly recommend not mapping things like mail messages (or almost anything) as collections. Query for them when you need them. See this blog entry for more info on this best practice:
http://blog.hibernate.org/cgi-bin/blosx ... ionmapping

-Chris

_________________
Chris Bredesen
Senior Software Maintenance Engineer, JBoss


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 06, 2007 5:37 pm 
Beginner
Beginner

Joined: Fri Jun 04, 2004 12:50 pm
Posts: 32
Hey Chris,

Thanks for the response. You bring up a point which I hadn't thought of before about using laziness when an object is in the session. I will do well to remember this!!

Unfortunately, in this case it is not in the HTTPSession. I use Struts and it is in the action where this is being called so the session is definitely not being closed (unless by something unusual which is forcing a close). What is interesting here is the code from the action being called uses the BookToSell lazy collection immediately before the call to the Mail lazy collection and it works fine.

I am literally asking for a count of the BooksToSell set and a count of the Emails set so that I can display those two values in the jsp page. It is these two values which I am placing into the session for use by the page.

Cheers
Tom


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.