-->
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.  [ 7 posts ] 
Author Message
 Post subject: illegal access to loading collection on one to many
PostPosted: Wed Aug 02, 2006 8:29 pm 
Newbie

Joined: Wed Oct 15, 2003 10:24 pm
Posts: 10
Could someone help me out on why I get the following exeception when trying to access the users set on my company?

Thanks!

org.hibernate.LazyInitializationException: illegal access to loading collection

Hibernate version: 3.0

Mapping documents:
<hibernate-mapping>
<class name="com.myorg.model.Company" table="company">

<id name="id" type="integer" column="company_id" >
<generator class="native"/>
</id>
<property name="name" type="string" column="name" not-null="true"/>
<set name="users" inverse="true" lazy="true">
<key column="company_id"/>
<one-to-many class="com.myorg.model.User"/>
</set>



</class>
</hibernate-mapping>

<hibernate-mapping>
<class name="com.myorg.model.User" table="[user]">

<id name="id" type="integer" column="user_id" >
<generator class="native"/>
</id>
<property name="user_name" type="string" column="user_name" not-null="true" unique="true"/>
<property name="email" type="string" column="email" not-null="true" unique="true"/>
<property name="first_name" type="string" column="first_name" not-null="true"/>
<property name="last_name" type="string" column="last_name" not-null="true"/>
<property name="er_optin" type="boolean" column="er_optin" not-null="false"/>
<property name="fi_optin" type="boolean" column="fi_optin" not-null="false"/>
<property name="ib_pm_optin" type="boolean" column="ib_pm_optin" not-null="false"/>
<property name="ib_ms_optin" type="boolean" column="ib_ms_optin" not-null="false"/>
<property name="ib_ma_optin" type="boolean" column="ib_ma_optin" not-null="false"/>
<property name="password" type="string" column="password" not-null="true"/>
<property name="passwordExpDate" type="date" column="password_exp_date" not-null="false"/>

<many-to-one name="company" column="company_id" class="com.myorg.model.Company" not-null="true"/>

<set name="roles"
lazy="true"

table="user_role">
<key column="user_id"/>
<many-to-many column="role_id" class="com.myorg.model.Role"/>
</set>

</class>
</hibernate-mapping>





Code between sessionFactory.openSession() and session.close():
Company company = (Company) getHibernateTemplate().get(Company.class, id);
Set users = company.getUsers();
int len = users.size();


Full stack trace of any exception that occurs:
org.hibernate.LazyInitializationException: illegal access to loading collection
[org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:172), org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:48), org.hibernate.collection.PersistentSet.hashCode(PersistentSet.java:324), org.apache.commons.lang.builder.HashCodeBuilder.append(HashCodeBuilder.java:297), org.apache.commons.lang.builder.HashCodeBuilder.reflectionHashCode(HashCodeBuilder.java:270), org.apache.commons.lang.builder.HashCodeBuilder.reflectionHashCode(HashCodeBuilder.java:200), org.apache.commons.lang.builder.HashCodeBuilder.reflectionHashCode(HashCodeBuilder.java:177), com.myorg.model.BaseObject.hashCode(BaseObject.java:27), org.apache.commons.lang.builder.HashCodeBuilder.append(HashCodeBuilder.java:297), org.apache.commons.lang.builder.HashCodeBuilder.reflectionHashCode(HashCodeBuilder.java:270), org.apache.commons.lang.builder.HashCodeBuilder.reflectionHashCode(HashCodeBuilder.java:200), org.apache.commons.lang.builder.HashCodeBuilder.reflectionHashCode(HashCodeBuilder.java:177), com.myorg.model.BaseObject.hashCode(BaseObject.java:27), java.util.HashMap.hash(Unknown Source), java.util.HashMap.put(Unknown Source), java.util.HashSet.add(Unknown Source), java.util.AbstractCollection.addAll(Unknown Source), org.hibernate.collection.PersistentSet.endRead(PersistentSet.java:242), org.hibernate.engine.CollectionLoadContext.endLoadingCollection(CollectionLoadContext.java:183), org.hibernate.engine.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:268), org.hibernate.engine.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:249), org.hibernate.loader.Loader.endCollectionLoad(Loader.java:554), org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:541), org.hibernate.loader.Loader.doQuery(Loader.java:436), org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:218), org.hibernate.loader.Loader.loadCollection(Loader.java:1434), org.hibernate.loader.collection.OneToManyLoader.initialize(OneToManyLoader.java:111), org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:488), org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:60), org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1430), org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:176), org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:48), org.hibernate.collection.PersistentSet.size(PersistentSet.java:110), com.myorg.dao.hibernate.CompanyDAOHibernate.get(CompanyDAOHibernate.java:103), com.myorg.service.impl.CompanyServiceImpl.getCompany(CompanyServiceImpl.java:27), sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method), sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source), sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source), java.lang.reflect.Method.invoke(Unknown Source), org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:287), org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:181), org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:148), org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96), org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:170), org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:176), $Proxy2.getCompany(Unknown Source), com.myorg.web.CompanyAction.edit(CompanyAction.java:105), sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method), sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source), sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source), java.lang.reflect.Method.invoke(Unknown Source), org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:270), org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:187), org.springframework.web.struts.DelegatingActionProxy.execute(DelegatingActionProxy.java:106), org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431), org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236), org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196), org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432), javax.servlet.http.HttpServlet.service(HttpServlet.java:709), javax.servlet.http.HttpServlet.service(HttpServlet.java:802), org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252), org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173), com.myorg.web.common.AuthorizationFilter.doFilter(AuthorizationFilter.java:105), org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202), org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173), org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:174), org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:77), org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202), org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173), org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213), org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178), org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126), org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105), org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107), org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148), org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869), org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664), org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527), org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80), org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684), java.lang.Thread.run(Unknown Source)



DB: SQL Server 2005


Top
 Profile  
 
 Post subject: RE: illegal access to loading collection on one to many
PostPosted: Wed Aug 02, 2006 11:03 pm 
Newbie

Joined: Wed Aug 02, 2006 2:58 am
Posts: 7
<hibernate-mapping>
<class name="com.myorg.model.Company" table="company">

<id name="id" type="integer" column="company_id" >
<generator class="native"/>
</id>
<property name="name" type="string" column="name" not-null="true"/>
<set name="users" inverse="true" lazy="true">
<key column="company_id"/>
<one-to-many class="com.myorg.model.User"/>
</set>

You are asking it to fetch the users lazily so whenever you try to access any property of the user from the set other than the Primary Key property it will throw you that exception. Please make the following change to the HBM file

<hibernate-mapping>
<class name="com.myorg.model.Company" table="company" lazy="false">

<id name="id" type="integer" column="company_id" >
<generator class="native"/>
</id>
<property name="name" type="string" column="name" not-null="true"/>
<set name="users" inverse="true" lazy="false" outer-join="true">
<key column="company_id"/>
<one-to-many class="com.myorg.model.User"/>
</set>


Let me know whether it works. There is a another alternative solution for the problem: add the following to the Java code:

Code:
Company company = (Company) getHibernateTemplate().get(Company.class, id);
[i]/*
I am not sure whether it can initialize a set directly
if it does not work please initialize every item in the set
using an iterator in similar fashion
*/[/i]
[b]Hibernate.initialize(company.getUsers());[/b]
Set users = company.getUsers();
int len = users.size();



Also please inform me whether this works.
Thank you[/code]


Top
 Profile  
 
 Post subject: Re: RE: illegal access to loading collection on one to many
PostPosted: Thu Aug 03, 2006 8:42 am 
Newbie

Joined: Wed Oct 15, 2003 10:24 pm
Posts: 10
Thanks for the quick response, but no that did not.

Just on an FYI too... I believe what you are stating there is incorrect. Lazy loading loading simply means that the set will not be loaded unless it is accessed. This will happen automatically when you access the set and will work without any extra effort as long as the session associated with it is still open (it's why Hibernate uses its own implementation of Sets). In my case I'm getting an illegal access exception while lazy loading (meaning that the session is open and trying to load but something is messing with my set while that's happening) and it can't load it up.


imyousuf wrote:
<hibernate-mapping>
<class name="com.myorg.model.Company" table="company">

<id name="id" type="integer" column="company_id" >
<generator class="native"/>
</id>
<property name="name" type="string" column="name" not-null="true"/>
<set name="users" inverse="true" lazy="true">
<key column="company_id"/>
<one-to-many class="com.myorg.model.User"/>
</set>

You are asking it to fetch the users lazily so whenever you try to access any property of the user from the set other than the Primary Key property it will throw you that exception. Please make the following change to the HBM file

<hibernate-mapping>
<class name="com.myorg.model.Company" table="company" lazy="false">

<id name="id" type="integer" column="company_id" >
<generator class="native"/>
</id>
<property name="name" type="string" column="name" not-null="true"/>
<set name="users" inverse="true" lazy="false" outer-join="true">
<key column="company_id"/>
<one-to-many class="com.myorg.model.User"/>
</set>


Let me know whether it works. There is a another alternative solution for the problem: add the following to the Java code:

Code:
Company company = (Company) getHibernateTemplate().get(Company.class, id);
[i]/*
I am not sure whether it can initialize a set directly
if it does not work please initialize every item in the set
using an iterator in similar fashion
*/[/i]
[b]Hibernate.initialize(company.getUsers());[/b]
Set users = company.getUsers();
int len = users.size();



Also please inform me whether this works.
Thank you[/code]


Top
 Profile  
 
 Post subject: RE: illegal access to loading collection on one to many
PostPosted: Thu Aug 03, 2006 12:37 pm 
Newbie

Joined: Wed Aug 02, 2006 2:58 am
Posts: 7
The exception JavaDoc says the following,
Quote:
Indicates access to unfetched data outside of a session context. For example, when an uninitialized proxy or collection is accessed after the session was closed

You probably saw that. That is why I said what I said. Never mind, what I said about Lazy fetching is assuming that the session was closed beforehand.

I got such error another time, it was when the Hibernate.initialize() or load or get was used to fetch an child attribute that can not fetched as it does not exist. Its just a guess. I found out this by printing the key of the object to be fetched lazily before I try to access any other property.
Please let me know once you solve the problem.

Thank you.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 11, 2006 1:58 pm 
Newbie

Joined: Wed Oct 15, 2003 10:24 pm
Posts: 10
OK... so my problem was this: someone on our team put a hashcode builder (org.apache.commons.lang.builder.HashCodeBuilder) in our base model object so that any object that inherits from it would produce a hashcode without any additional code at the child objects.

Sounds nice, but when loading a set of an object that uses this builder for its hash code, it causes illegal access exceptions. Guess you just need to build that hashcode manually if you need one (i.e. can't just get away the session keeping track of the object).


Top
 Profile  
 
 Post subject: Re: illegal access to loading collection on one to many
PostPosted: Fri Jul 17, 2009 7:58 pm 
Newbie

Joined: Fri Jul 17, 2009 7:18 pm
Posts: 1
There was nothing wrong with HashCode in my case. The problem was in a database data itself. For some reason referenced records had been deleted from the table, so, I got a circumstances when record in one table contains reference to a record that is not exists in another table.
Interesting, but collection I was trying to get was not related to any of "broken" reference.
I solved the problem by adding not-found="ignore"

Code:
<class name="com.myorg.model.User" table="user">
    <id name="id" type="integer" column="user_id" >
    <version name="version" column="version" insert="true"/>
    <property name="name" column="name"/>

    <set name="roles" table="user_role" lazy="true" cascade="none" batch-size="100" >
      <key column="user_id" />
      <many-to-many class="com.myorg.model.Role" column="role_id" not-found="ignore"/>
    </set>
</class>


Another reason why you might have this exception is one of the object in a collection that can't be initialized (missing required data or referenced object is not found). That causes the entire collection fail to initialize.


Top
 Profile  
 
 Post subject: Re: illegal access to loading collection on one to many
PostPosted: Wed Jan 22, 2014 5:27 pm 
Newbie

Joined: Fri Mar 19, 2010 2:37 pm
Posts: 5
I had the same issue.

As "paulj" said, hashcode what my issue also.
I had a Set in the hashcode calculation. I removed the Set from the hashcode and now this error Gone !!.

Thanks paulj.

_________________
Venod Raveendran
Architect-Technology
[color=#BF4040]Congizant Technology Solutions[/color]


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