-->
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 Error while updating
PostPosted: Fri Jun 06, 2008 7:19 am 
Newbie

Joined: Thu Jun 05, 2008 11:45 pm
Posts: 6
Hi,
I am new to hibernate and am using version 3.3 with Oracle 10g as DB.
I have two tables ICP and ICPCategory. ICP has field 'ICPCode' which is set as foreign key in ICPCategory.

An entry is present in the ICPCategory table with the foreign key value ICPCode from ICP (I001).

Now when I try to update the the record in ICP which has ICPCode I001 it results in lazy initialization error.
This seems to happen due to the FK relationship. But I am not changing the value of ICPCode. This happens even if I try to update without changing any field value. I am using merge() method of hibernate to update the record.

Could anyone please help me with this issue. Very Urgent.

Thanks & Regards
Ajay

Error Stack Trace
---------------------
org.springframework.orm.hibernate3.HibernateSystemException: illegal access to loading collection; nested exception is org.hibernate.LazyInitializationException: illegal access to loading collection 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:659) at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412) at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:378) at org.springframework.orm.hibernate3.HibernateTemplate.merge(HibernateTemplate.java:769) at com.cpt.his.dao.hibernate.UniversalDaoHibernate.save(UniversalDaoHibernate.java:31) at com.cpt.his.service.impl.UniversalManagerImpl.save(UniversalManagerImpl.java:57) 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:301) at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149) at org.acegisecurity.intercept.method.aopalliance.MethodSecurityInterceptor.invoke(MethodSecurityInterceptor.java:66) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 06, 2008 11:46 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
The problem is that you're closing the session and committing the transaction, despite the fact that you still need access to the objects that have been pulled from the database.

Keep the session open a bit longer, and you won't run into this problem. Take a look at the open session in view pattern and you'll get some ideas on how to solve this lazy loading exception.

Here's a little tutorial I put together on How Hibernate Works. You might find it useful, as it addresses the problem with lazy loading.

http://jpa.ezhibernate.com/Javacode/learn.jsp?tutorial=07howhibernateworks

Another option might just to be do eager loading on the entity so everything is loaded at once. Not what I like to do, but you can try it.

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 09, 2008 12:32 am 
Newbie

Joined: Thu Jun 05, 2008 11:45 pm
Posts: 6
Hi,
Thanks for your suggestions. I looked in Open session and am a bit confused. In some places the solution seems to be to use a Session View Interceptor and in some cases the solution has been given as implementing a Session Filter. I am not sure which solution should I go in for? Could you please help me out in this?
Thanks
Ajay


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 09, 2008 9:14 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
I would say creating a ServletFilter that opens a transaction as the request comes in, and closes the transaction/Session as the response goes out is probably the easiest way to implement this behavior.

-Cameron McKenzie

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 09, 2008 11:28 pm 
Newbie

Joined: Thu Jun 05, 2008 11:45 pm
Posts: 6
Hi,
I tried extending the OpenSessionInViewFilter. It seems to work and for now the lazyinitialization error is not coming. Am checking more on it.
Thanks a lot for your help.

Ajay


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 11, 2008 12:40 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Awesome! This is a very annoying exception to get, but once you understand why you're getting it, and how to avoid it, you won't have any problem avoiding it in the future.

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 12, 2008 12:46 am 
Newbie

Joined: Thu Jun 05, 2008 11:45 pm
Posts: 6
Hi,
The error has cropped up again. It works in some cases and does not in others.
I always get the error in the hashCode() method that I have implemented in my POJO's. So would it be a good idea to remove the lazily loaded objects from the hashCode() method. In that case what would be the side effects? Similarly would I need to remove the lazy objects from the toString() and equals() method also.

Any ideas would be greatly appreciated.

I have posted this doubt of mine as a separate topic.
http://forum.hibernate.org/viewtopic.php?t=987709


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.