-->
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.  [ 8 posts ] 
Author Message
 Post subject: Bidirectional many-to-many lazy = false
PostPosted: Sun Oct 12, 2008 9:25 am 
Newbie

Joined: Sun Oct 12, 2008 9:17 am
Posts: 9
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.3.1

Mapping documents:
<hibernate-mapping>

<class name="model.PadmOrgano" table="PADM_ORGANOS">

<id name="idOrgano"
type="long">
<column name="ID_ORGANO"/>
</id>
<set name="pdatRegistrosPads"
table="PREL_REGISTROS_ORGANOS" lazy="false" >
<key column="ID_ORGANO"/>
<many-to-many class="model.PdatRegistro" column="ID_REGISTRO"
lazy="false"/>
</set>
</class>

</hibernate-mapping>
----- ------
<hibernate-mapping>

<class name="model.PdatRegistro" table="PDAT_REGISTROS">

<id name="idRegistro"
type="long">
<column name="ID_REGISTRO"/>
</id>

<set name="padmOrganosPdas" inverse="true" table="PREL_REGISTROS_ORGANOS" lazy="false">
<key column="ID_REGISTRO"/>
<many-to-many class="model.PadmOrgano" column="ID_ORGANO"
lazy="false"/>
</set>
</class>



Code between sessionFactory.openSession() and session.close():

making a "get" operation


Full stack trace of any exception that occurs:

LazyInitializationError

Name and version of the database you are using: Oracle 11

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Problems with Session and transaction handling?

Read this: http://hibernate.org/42.html

I´m developing a web application with spring and hibernate 3.3.1. I have a bidirectional many-to-many association, lazy = false. I make mappings you can see above, but I have LazyInitializationError. I have done a lot of test but ever the same result. I have read in the reference and in the book hibernate in action but I can't reach a solution.

I would appreciate any help, thanks in advance

Rodolfo


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 12, 2008 1:37 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
You have not set the lazy attribute for your <class> declarations. The default is lazy="true" so it may help to explicitly set <class ... laze="false">.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 13, 2008 4:47 pm 
Newbie

Joined: Sun Oct 12, 2008 9:17 am
Posts: 9
Thanks a lot but it doesn't work, I have the same error.

Thanks anyway

Rodolfo


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 14, 2008 1:51 am 
Newbie

Joined: Sun Oct 12, 2008 9:17 am
Posts: 9
I use Spring 2.0. Anybody think that it could be the problem? it would be necesary to update to 2.5?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 14, 2008 1:59 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Quote:
making a "get" operation


I think would be useful to see your code that leads to the exception. Also post the FULL stacktrace that you are seeing. If all your classes and all your collections has lazy="false" then you should never get lazy initialization errors. But it is probably not a very good idea to have everything non-lazy since it would more or less load your entire database every time you do something.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 14, 2008 1:25 pm 
Newbie

Joined: Sun Oct 12, 2008 9:17 am
Posts: 9
This is the stackTrace

javax.servlet.ServletException: org.springframework.orm.hibernate3.HibernateSystemException: illegal access to loading collection; nested exception is org.hibernate.LazyInitializationException: illegal access to loading collection
at org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:523)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:421)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:179)
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:262)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:446)
at java.lang.Thread.run(Thread.java:619)
Caused by: org.springframework.orm.hibernate3.HibernateSystemException: illegal access to loading collection; nested exception is org.hibernate.LazyInitializationException: illegal access to loading collection
at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:661)
at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:424)
at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
at org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:917)
at com.mdagen.rallie.dao.hibernate.EquipoDAOHibernate.getEquipos(EquipoDAOHibernate.java:99)
at com.mdagen.rallie.service.impl.EquipoManagerImpl.getEquipos(EquipoManagerImpl.java:59)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:310)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
at $Proxy73.getEquipos(Unknown Source)
at com.mdagen.rallie.action.MarcaInitialLoadAction.execute(MarcaInitialLoadAction.java:40)
at org.springframework.web.struts.DelegatingActionProxy.execute(DelegatingActionProxy.java:110)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:419)
... 23 more
Caused by: org.hibernate.LazyInitializationException: illegal access to loading collection
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:363)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:108)
at org.hibernate.collection.PersistentSet.hashCode(PersistentSet.java:434)
at org.apache.commons.lang.builder.HashCodeBuilder.append(HashCodeBuilder.java:392)
at org.apache.commons.lang.builder.HashCodeBuilder.reflectionAppend(HashCodeBuilder.java:353)
at org.apache.commons.lang.builder.HashCodeBuilder.reflectionHashCode(HashCodeBuilder.java:327)
at org.apache.commons.lang.builder.HashCodeBuilder.reflectionHashCode(HashCodeBuilder.java:194)
at com.mdagen.commons.model.BaseObject.hashCode(BaseObject.java:22)
at java.util.HashMap.put(HashMap.java:372)
at java.util.HashSet.add(HashSet.java:200)

I have changed to spring 2.5.5 and i´m using hibernate 3.3.1 in Jboss 4.2.2.

the hql is

"from equipo as ta"

the classes aren`t the same but the problem is the same. Because I can't acces to the first application which is in oracle and this is in Mysql. But the problem and the mapping and the style of code is the same.

Thanks in advance,

Rodolfo


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 14, 2008 5:29 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Ok, I have checked the source of this error in the Hibernate code, and it seems like the error is about trying to use the collection before it has been properly initialized. I suspect that the source of the problem is the org.apache.commons.lang.builder.HashCodeBuilder which you are using in your BaseObject code. I don't know what is going on in that method or in the HashCodeBuilder, but I suspect that it goes something like this:

1. You are calling a method that triggers an initialization of the collection
2. Hibernate starts with the initialization
3. Hibernate needs the hash code, since it is putting the almost initialized objects in a Set.
4. But the hashCode() method is overridden in BaseObject and the code in it ends up trying to access the collection that is not yet fully initialized.
5. Ouch, Hibernate detects the second attempt to initialize the same collection and throws the exception

I guess the fix is to make sure the hash code calculation doesn't trigger loading of any collections or associations.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 15, 2008 4:02 pm 
Newbie

Joined: Sun Oct 12, 2008 9:17 am
Posts: 9
Thanks, that is the problem, I have changer BaseObject and it works perfectly.

Thanks a lot

Rodolfo


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 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.