-->
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.  [ 14 posts ] 
Author Message
 Post subject: Parent/child works in POJO-mode, but not in MAP-mode -- bug?
PostPosted: Fri Jun 24, 2005 5:38 pm 
Beginner
Beginner

Joined: Wed Feb 09, 2005 3:27 pm
Posts: 29
I got a LazyInitializationException when attempting to map a parent-child relationship so I created a test using the example at http://www.hibernate.org/hib_docs/refer ... tions.html (section 6.8). The test passes when I use POJOs, but fails (LazyInitializationException) when I use maps. Is this a mistake on my part, a bug, or something that needs to be worked around? Let me know if you want me to post the code. Lazy loading has been turned off for both entities and inverse="true" has been specified for the parent. Any help/suggestions are greatly appreciated!

Thanks,

-Kaare


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 26, 2005 4:01 pm 
Newbie

Joined: Mon May 30, 2005 10:26 am
Posts: 13
Interesting, in the bad way...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 27, 2005 11:05 am 
Beginner
Beginner

Joined: Wed Feb 09, 2005 3:27 pm
Posts: 29
acarstoiu wrote:
Interesting, in the bad way...


Yes, this wasn't what I expected. :-) Anyway, when I remove the relationship from the child to the parent (i.e. the parent attribute on the child entity) it works.

To the Hibernate-team: could you please let me know if this sounds like a bug and if I should enter it into JIRA? Or do you need more details to make that call? Please let me know.

Thanks,

-Kaare


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 27, 2005 11:23 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
what is the complete stacktrace ?

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 27, 2005 11:35 am 
Beginner
Beginner

Joined: Wed Feb 09, 2005 3:27 pm
Posts: 29
Here's the SQL plus the stack trace:

Code:
Hibernate: select parent0_.PARENT_ID as PARENT1_0_ from PARENT parent0_ where parent0_.PARENT_ID=?
Hibernate: select children0_.parent_id as parent2_1_, children0_.CHILD_ID as CHILD1_1_, children0_.CHILD_ID as CHILD1_0_, children
0_.parent_id as parent2_1_0_ from CHILD children0_ where children0_.parent_id=?
Exception in thread "main" org.hibernate.LazyInitializationException: illegal access to loading collection
        at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:172)
        at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:48)
        at org.hibernate.collection.PersistentSet.hashCode(PersistentSet.java:324)
        at java.util.HashMap$Entry.hashCode(HashMap.java:707)
        at java.util.AbstractMap.hashCode(AbstractMap.java:556)
        at java.util.HashMap$Entry.hashCode(HashMap.java:707)
        at java.util.AbstractMap.hashCode(AbstractMap.java:556)
        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:242)
        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:554)
        at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:541)
        at org.hibernate.loader.Loader.doQuery(Loader.java:436)
        at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:218)
        at org.hibernate.loader.Loader.loadCollection(Loader.java:1434)
        at org.hibernate.loader.collection.OneToManyLoader.initialize(OneToManyLoader.java:111)
        at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:488)
        at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:60)
        at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1430)
        at org.hibernate.collection.AbstractPersistentCollection.forceInitialization(AbstractPersistentCollection.java:280)
        at org.hibernate.engine.PersistenceContext.initializeNonLazyCollections(PersistenceContext.java:796)
        at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:223)
        at org.hibernate.loader.Loader.loadEntity(Loader.java:1345)
        at org.hibernate.loader.entity.EntityLoader.load(EntityLoader.java:116)
        at org.hibernate.loader.entity.EntityLoader.load(EntityLoader.java:101)
        at org.hibernate.persister.entity.BasicEntityPersister.load(BasicEntityPersister.java:2471)
        at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:351)
        at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:332)
        at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:113)
        at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:167)
        at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:79)
        at org.hibernate.impl.SessionImpl.get(SessionImpl.java:621)
        at test.ParentChildTest.main(ParentChildTest.java:24)


The mapping:

Code:
  <class entity-name="Parent" table="PARENT">
    <id name="id" column="PARENT_ID" type="integer"/>
    <set name="children" inverse="true" lazy="false" >
      <key column="parent_id"/>
      <one-to-many entity-name="Child"/>
    </set>
  </class>
  <class entity-name="Child" table="CHILD">
    <id name="id" column="CHILD_ID" type="integer"/>
    <many-to-one name="parent" entity-name="Parent" column="parent_id" lazy="false" not-null="true" />
  </class>



The call (ParentChildTest.java lines 23-24):

Code:
        Session session = sessionFactory.openSession().getSession(EntityMode.MAP);
        Object object = session.get("Parent", new Integer(1));


Thanks,

-Kaare


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 29, 2005 4:00 pm 
Beginner
Beginner

Joined: Wed Feb 09, 2005 3:27 pm
Posts: 29
Max,

Have you had a chance to take a look at this? I'd greatly appreciate your verdict on this one.

Thanks,

-Kaare


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 29, 2005 4:07 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
you have a recursive structure - this is impossible to have in a HashMap (try to call hashcode() on it ,)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 29, 2005 4:25 pm 
Beginner
Beginner

Joined: Wed Feb 09, 2005 3:27 pm
Posts: 29
Are you saying that bi-directional relationships aren't supported in MAP-mode due to the implementation of HashMap.hashCode()? Is it possible to work around this, e.g. is it possible to have Hibernate use a different Map implementation?

Thanks,

-Kaare


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 29, 2005 4:28 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
no - not possible afaik.

contributions welcome (even though i dont think it is very doable because of the nature of equals and hashcode - read not hibernate specific ;)

/max

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 01, 2005 2:00 pm 
Newbie

Joined: Mon May 30, 2005 10:26 am
Posts: 13
Can you both check this, please:
http://forum.hibernate.org/viewtopic.ph ... highlight=
Thanks in advance, max.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 01, 2005 2:38 pm 
Beginner
Beginner

Joined: Wed Feb 09, 2005 3:27 pm
Posts: 29
acarstoiu wrote:
Can you both check this, please:
http://forum.hibernate.org/viewtopic.ph ... highlight=
Thanks in advance, max.


Interesting. Calling hashCode() and equals() works, but toString() creates a stack overflow. This is for a small, Java-only test. I made the change you mentioned in the other post and I'm able to run the previous test successfully. Very interesting.

Max, any idea if changing the implementation to use an IdentityHashMap instead of a HashMap will break anything?

Thanks, acarstoiu!

-Kaare


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 01, 2005 11:43 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Yes, it'll break jvm backward compatibility. IdentityHashMap is only available since jdk1.4

There is work planned to let you plug in your own map implementations; see:
http://opensource.atlassian.com/projects/hibernate/browse/HHH-295


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 05, 2005 10:16 am 
Beginner
Beginner

Joined: Wed Feb 09, 2005 3:27 pm
Posts: 29
That's great news! Thanks guys!

-Kaare


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 19, 2005 10:33 am 
Beginner
Beginner

Joined: Wed Feb 09, 2005 3:27 pm
Posts: 29
Steve,

I see that HHH-295 is closed and that the change is in 3.1 beta 3. Is there a write-up of how to use this feature anywhere? I looked at the issue details, but couldn't find anything that describes what was changed, etc.

Thanks,

-Kaare


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