-->
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.  [ 4 posts ] 
Author Message
 Post subject: Collection of Collection with inverse=true
PostPosted: Fri Mar 20, 2015 11:30 am 
Newbie

Joined: Fri Mar 20, 2015 11:13 am
Posts: 3
I have configure my hbm.xml with 2 collections inside : a collection of a collection with inverse=true for the two.
<class class="C0">
<properties/>
<set inverse="true">
<one-to-many class="C1"/>
</set>

</class>

<class class="C1">
<properties/>
<set inverse="true" >
<one-to-many class="C2"/>
</set>

</class>

<class class="C2">
...<!-- No collection there -->
</class>

When i fecth my father collection C0 through SETs i find good number of elements inside my C1 collection BUT not all item inside my C2 collection which contains only one element per item (waiting for 2 elements per each item).

Is it a bug ? or am i wrong ?

I can send you my file if that's can help.

Hibernate version : hibernate-core-4.3.8.Final.jar
C3P0 : hibernate-c3p0-4.3.8.Final.jar
JAVA 8.0


Thank you in advance.


Top
 Profile  
 
 Post subject: Re: Collection of Collection with inverse=true
PostPosted: Sat Mar 21, 2015 3:17 pm 
Newbie

Joined: Tue Mar 17, 2015 9:53 pm
Posts: 4
You use set inverse on any side of the many-to-many association. But it's very clear for one-to-many. In this case, you have it on C0 and also on C1. This might be confusing Hibernate.

From Hibernate docs: "The rules are straightforward: all bi-directional associations need one side as inverse. In a one-to-many association it has to be the many-side, and in many-to-many association you can select either side." (at the end of this section: http://docs.jboss.org/hibernate/orm/4.3/manual/en-US/html/ch01.html#tutorial-associations-usingbidir)

Try to move the set inverse on C0 to C2 to see if that solves the problem.


Top
 Profile  
 
 Post subject: Re: Collection of Collection with inverse=true
PostPosted: Mon Mar 23, 2015 4:32 am 
Newbie

Joined: Fri Mar 20, 2015 11:13 am
Posts: 3
Hi raminr,

Thank u very much for your answer. Did you mean that ?

<class class="C0">
<properties/>
...<!-- No collection there -->
</class>

<class class="C1">
<properties/>
<set inverse="true" >
<one-to-many class="C2"/>
</set>
</class>

<class class="C2">
<set inverse="true">
<one-to-many class="C1"/>
</set>

</class>


Top
 Profile  
 
 Post subject: Re: Collection of Collection with inverse=true
PostPosted: Mon Mar 23, 2015 5:10 am 
Newbie

Joined: Fri Mar 20, 2015 11:13 am
Posts: 3
Got it !!!

The problem occurs because of my equals method on class C2'ID where i have forgotten an attribute.

Example for explanation.

>> My object to be compared has this composed id : id1 (of class C0), id2 (of class C1), id3 (of class C2).

>> and my equals method of class ID of C2 was like this
id1.equals(other.id1) && id2.equals(other.id2)

so when hibernate get to database, C2 objects (id1, id2, id3), he compares hese ids and says that obj1.id equals to obj2.id which is true the two objects are the same. So hibernate chooses only one of two objects. This is why i had one object.

SOLUTION : I add to my equals method of class ID of object C2 the lacking id3 and everything gones GOOD.
id1.equals(other.id1) && id2.equals(other.id2) && id3.equals(other.id3)

So to sum up :
  • With hibernate you can make collection of collection [of collection] with inverse=true as you want.
  • Don't forget to override equals and hashcode for collections. This garantee the uniqueness for your objects and tell hibernate how to manage with them.
  • The most important: be careful when you write you equals and hashcode method! don't forget any IDs to be compared :-)

That's all.
Thank u again raminr for your answer


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