-->
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: Assosiation problems
PostPosted: Fri Oct 13, 2006 2:26 am 
Newbie

Joined: Wed Aug 02, 2006 10:17 am
Posts: 12
Hi everybody.
I'm having problems with my bidirectional assosiations.
Im working with
Hibernate version: 3.1.3

I have two tables. VK_ORG and USER_VK_ORG. One VK_ORG has lots of users. And one user has one VK_ORG.

Mapping documents:
<hibernate-mapping package="sto.app.mm.shoppingcart.data.model">

<class name="User_Vk_Org" table="USER_VK_ORG" lazy="false">

<composite-id>
<key-property name="login" column="LOGIN" length="6" type="java.lang.String"/>
<key-property name="vk_Org_Id" column="VK_ORG" type="java.lang.Integer" />
</composite-id>

<many-to-one name="vk_Org"
class="Vk_Org"
cascade="none"
fetch="select"
column="VK_ORG"
not-null="true"
insert="false" update="false">
</many-to-one>

</class>


<class name="Vk_Org" table="VK_ORG" lazy="false">

<id name="vk_org" column="VK_ORG" type="java.lang.Integer">
<generator class="assigned"/>
</id>

<property name="bezeichnung"
column="BEZEICHNUNG"
length="50"
type="java.lang.String"
not-null="true"/>

<set name="users" lazy="true" cascade="all" inverse="true">
<key column="VK_ORG"/>
<one-to-many class="User_Vk_Org"/>
</set>

</class>

</hibernate-mapping>



I wrote a test. For the moment I dont want to access any documents in the set of VK_ORG. But still there are exeptions.


myFactory.getCurrentSession().beginTransaction();
Vk_Org pojo = (Vk_Org) session.get(Vk_Org.class, (Integer) id); //id is a valid primary key
myFactory.getCurrentSession().getTransaction().commit();


When I run this I get exeptions.

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: sto.app.mm.shoppingcart.data.model.Vk_Org.users, no session or session 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.initialize(AbstractPersistentCollection.java:343)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)
at org.hibernate.collection.PersistentSet.equals(PersistentSet.java:350)
at org.apache.commons.lang.builder.EqualsBuilder.append(EqualsBuilder.java:296)
at org.apache.commons.lang.builder.EqualsBuilder.reflectionAppend(EqualsBuilder.java:245)
at org.apache.commons.lang.builder.EqualsBuilder.reflectionEquals(EqualsBuilder.java:205)
at org.apache.commons.lang.builder.EqualsBuilder.reflectionEquals(EqualsBuilder.java:121)
at sto.app.mm.shoppingcart.data.model.BasePojo.equals(BasePojo.java:12)
at sto.app.mm.shoppingcart.data.test.Vk_OrgTest.testget(Vk_OrgTest.java:78)
at sto.app.mm.shoppingcart.data.test.Vk_OrgTest$1.runTest(Vk_OrgTest.java:32)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:436)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:311)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)


I read that if I switch lazy loading off, It might work. but if I do so I get another exception:
org.hibernate.LazyInitializationException: illegal access to loading collection
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:341)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)
at org.hibernate.collection.PersistentSet.hashCode(PersistentSet.java:355)
at org.apache.commons.lang.builder.HashCodeBuilder.append(HashCodeBuilder.java:348)
at org.apache.commons.lang.builder.HashCodeBuilder.reflectionAppend(HashCodeBuilder.java:309)
at org.apache.commons.lang.builder.HashCodeBuilder.reflectionHashCode(HashCodeBuilder.java:283)
at org.apache.commons.lang.builder.HashCodeBuilder.reflectionHashCode(HashCodeBuilder.java:150)
at sto.app.mm.shoppingcart.data.model.BasePojo.hashCode(BasePojo.java:16)
at org.apache.commons.lang.builder.HashCodeBuilder.append(HashCodeBuilder.java:348)
at org.apache.commons.lang.builder.HashCodeBuilder.reflectionAppend(HashCodeBuilder.java:309)
at org.apache.commons.lang.builder.HashCodeBuilder.reflectionHashCode(HashCodeBuilder.java:283)
at org.apache.commons.lang.builder.HashCodeBuilder.reflectionHashCode(HashCodeBuilder.java:150)
at sto.app.mm.shoppingcart.data.model.BasePojo.hashCode(BasePojo.java:16)
at java.util.HashMap.hash(HashMap.java:261)
at java.util.HashMap.put(HashMap.java:379)
at java.util.HashSet.add(HashSet.java:192)
at java.util.AbstractCollection.addAll(AbstractCollection.java:319)
at org.hibernate.collection.PersistentSet.endRead(PersistentSet.java:273)
at org.hibernate.engine.CollectionLoadContext.endLoadingCollection(CollectionLoadContext.java:183)
at org.hibernate.engine.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:268)
at org.hibernate.engine.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:249)
at org.hibernate.loader.Loader.endCollectionLoad(Loader.java:866)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:853)
at org.hibernate.loader.Loader.doQuery(Loader.java:717)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
at org.hibernate.loader.Loader.loadCollection(Loader.java:1919)
at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:36)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:520)
at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:60)
at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1676)
at org.hibernate.collection.AbstractPersistentCollection.forceInitialization(AbstractPersistentCollection.java:454)
at org.hibernate.engine.StatefulPersistenceContext.initializeNonLazyCollections(StatefulPersistenceContext.java:755)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:229)
at org.hibernate.loader.Loader.loadEntity(Loader.java:1785)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:47)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:41)
at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:2730)
at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:365)
at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:346)
at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:123)
at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:161)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:87)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:862)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:799)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:792)
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:324)
at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:301)
at $Proxy0.get(Unknown Source)
at sto.app.mm.shoppingcart.data.dao.Vk_OrgDao.getInstance(Vk_OrgDao.java:26)
at sto.app.mm.shoppingcart.data.test.Vk_OrgTest.testget(Vk_OrgTest.java:76)
at sto.app.mm.shoppingcart.data.test.Vk_OrgTest$1.runTest(Vk_OrgTest.java:32)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:436)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:311)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)


I read about this problem that there might be something with my equals or/and hashcode implementation. I use the Equalbuilder of the apache project using the reflection mechanism to do the equals/hashcode.

I use DB2 8.1

Please help me, I'm really stuck. cant be such a big problem, cause the thing I'm trying to do seems quite common to me.

Thank you very much,

Stefan


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 13, 2006 3:45 am 
Newbie

Joined: Wed Aug 02, 2006 10:17 am
Posts: 12
I actually accessed the pojo after closing of the ssession. I removesd it, but it doesnt solve the error.
When I access an object within the collection I still get the follwing exception:
    org.hibernate.LazyInitializationException: illegal access to loading collection
    at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:341)
    at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)
    at org.hibernate.collection.PersistentSet.hashCode(PersistentSet.java:355)
    at org.apache.commons.lang.builder.HashCodeBuilder.append(HashCodeBuilder.java:348)
    at org.apache.commons.lang.builder.HashCodeBuilder.reflectionAppend(HashCodeBuilder.java:309)
    at org.apache.commons.lang.builder.HashCodeBuilder.reflectionHashCode(HashCodeBuilder.java:283)
    at org.apache.commons.lang.builder.HashCodeBuilder.reflectionHashCode(HashCodeBuilder.java:150)
    at sto.app.mm.shoppingcart.data.model.BasePojo.hashCode(BasePojo.java:16)
    at org.apache.commons.lang.builder.HashCodeBuilder.append(HashCodeBuilder.java:348)
    at org.apache.commons.lang.builder.HashCodeBuilder.reflectionAppend(HashCodeBuilder.java:309)
    at org.apache.commons.lang.builder.HashCodeBuilder.reflectionHashCode(HashCodeBuilder.java:283)
    at org.apache.commons.lang.builder.HashCodeBuilder.reflectionHashCode(HashCodeBuilder.java:150)
    at sto.app.mm.shoppingcart.data.model.BasePojo.hashCode(BasePojo.java:16)
    at java.util.HashMap.hash(HashMap.java:261)
    at java.util.HashMap.put(HashMap.java:379)
    at java.util.HashSet.add(HashSet.java:192)
    at java.util.AbstractCollection.addAll(AbstractCollection.java:319)
    at org.hibernate.collection.PersistentSet.endRead(PersistentSet.java:273)
    at org.hibernate.engine.CollectionLoadContext.endLoadingCollection(CollectionLoadContext.java:183)
    at org.hibernate.engine.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:268)
    at org.hibernate.engine.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:249)
    at org.hibernate.loader.Loader.endCollectionLoad(Loader.java:866)
    at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:853)
    at org.hibernate.loader.Loader.doQuery(Loader.java:717)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
    at org.hibernate.loader.Loader.loadCollection(Loader.java:1919)
    at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:36)
    at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:520)
    at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:60)
    at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1676)
    at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:344)
    at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)
    at org.hibernate.collection.PersistentSet.iterator(PersistentSet.java:138)
    at sto.app.mm.shoppingcart.data.test.Vk_OrgTest.testget(Vk_OrgTest.java:79)
    at sto.app.mm.shoppingcart.data.test.Vk_OrgTest$1.runTest(Vk_OrgTest.java:33)
    at junit.framework.TestCase.runBare(TestCase.java:127)
    at junit.framework.TestResult$1.protect(TestResult.java:106)
    at junit.framework.TestResult.runProtected(TestResult.java:124)
    at junit.framework.TestResult.run(TestResult.java:109)
    at junit.framework.TestCase.run(TestCase.java:118)
    at junit.framework.TestSuite.runTest(TestSuite.java:208)
    at junit.framework.TestSuite.run(TestSuite.java:203)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:436)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:311)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)


I kept attention not to access the objkect when the session is closed. So that cant be the mistake here.
Any hints?

Thank you,
Stefan


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 13, 2006 4:04 am 
Beginner
Beginner

Joined: Fri Oct 28, 2005 7:11 am
Posts: 20
Do you have any code in the hibernate events? If so, are you sure you don't do any strange operations there (modification , query, etc)?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 13, 2006 4:26 am 
Newbie

Joined: Wed Aug 02, 2006 10:17 am
Posts: 12
Hi tbech,

I don't think I do strange things in the Hibernate events. I think I postet all relevant code. I didnt post how I create the sessionFactory, which was quite straightforward
Code:
sessionFactory = new Configuration().configure().buildSessionFactory();


What exactly do you mean when you say "doing strange stuff".


Top
 Profile  
 
 Post subject: I solved this!
PostPosted: Sun Oct 15, 2006 4:37 am 
Newbie

Joined: Sun Oct 15, 2006 4:13 am
Posts: 2
Hopefully you might have fixed this. I came across this in Google when I was encountering the same error. Nowhere I could get a solution for this error and I am posting this so that this is helpful to someone else. With trial and error, I realized that the hashCode() method was the one that is creating the problem - specifically the apache commons HashCodeBuilder (and also the the EqualsBuilder / ToStringBuilder).

This happens especially when you have some base persistable object class that implements the equals(), the hashCode() or the toString() methods using the apache commons-lang (in the above case BasePOJO class). For all your hibernate objects, you extend the base persistable class. All your objects stored in one-to-many relationship fields are accessed by the HashCodeBuilder to create hashCode() and that creates this problem. For example Role has "User user" and User has "Set<Role> roles". Now when a hashCode is called for a Role object, a hashCode() call is made by the HashCodeBuilder on all properties of role... So a hashCode() is called for user... which in turn makes a hashCode() call on Set<Role>... which in turn calls hashCode() on all elements of HashMap... that means hashCode() called for all Role associated to user... and so on...

Now there are two solutions:
1. Declare your Set class fields (one to many relationship fields) transient
2. Write your own equals(), toString() and hashCode() methods in the POJO instead of the Base POJO.


-- Hari Gangadharan


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 16, 2006 4:32 am 
Newbie

Joined: Wed Aug 02, 2006 10:17 am
Posts: 12
Thank you so much!

You saved my head! Now its working.


Thanks again!
Stefan


Top
 Profile  
 
 Post subject: You are welcome!
PostPosted: Wed Oct 18, 2006 1:40 am 
Newbie

Joined: Sun Oct 15, 2006 4:13 am
Posts: 2
It has been an old thread and I never thought this will be useful to you... Glad that I could help.


(just noticed that this is not an old post -- I was looking at your join date and mistook it as the thread post date)

Regards,

-- Hari Gangadharan


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.