-->
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.  [ 5 posts ] 
Author Message
 Post subject: Duplicate rows in a many to many relation
PostPosted: Mon Mar 16, 2009 10:13 am 
Newbie

Joined: Thu Dec 21, 2006 8:06 am
Posts: 8
Hi,

I'm having an issue with a many-to-many relation.

I have 2 tables, PLAYER, and CARD.

A party, can have several cards.
A card, can be played by several players.
So far, a basic many-to-many relation if I'm not mistaken.
But! and here is my problem, a player can have several times the same card.

So, when I create my many-to-many relation, a liaison table PLAYERCARD is created, with 2 fields (playerId and CardId).

When I do player.getCards(), the issue is that if the player have several times the same card, the collection will return several times the same object (same reference) when, actually, it's not the same object but 2 clones.

How could I map that in an other way? Is it possible for instance, to have an Id for the table PLAYERCARD that I could map in the Card class to differenciate them?

Thanks a lot for your help,

Guillaume


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 17, 2009 2:09 pm 
Newbie

Joined: Thu Dec 21, 2006 8:06 am
Posts: 8
Let me know if I'm not clear.

I'm sure this case of having a many to many relation with duplicate rows is well known but I can't find a solution on internet so far.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 17, 2009 3:17 pm 
Regular
Regular

Joined: Tue Dec 30, 2008 8:14 pm
Posts: 50
When a Player has two instances of the same card, do you want to get two objects card1 and card2 so that card1.equals(card2) is true, but card1 == card2 is false?

LinHib.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 17, 2009 3:20 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
With a <bag> mapping it is possible for a player to have duplicate references to the same card.

But it will always be a reference to the same object instance. It is a requirement by Hibernate that in a given session an entity can only have one object representation.

You can find some examples of <bag> here:
http://www.hibernate.org/hib_docs/v3/re ... s-ofvalues

and here is some information about object identity:
http://www.hibernate.org/hib_docs/v3/re ... s-identity


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 08, 2009 11:18 am 
Newbie

Joined: Thu Dec 21, 2006 8:06 am
Posts: 8
Hi,

thanks a lot for your replies!

LinHib -> yes this is exactly what I want. only solution I found till now is to clone manually the object but that can't be the best solution.
I understand that, on a database point of view, so on hibernate point of view, it's the same object, but I believe this case must have been spotted by hibernate team. May be an option that would allow me to have a unique ID for my "link table" that I could have to my Card object?

I'll let you know if I find something. thx for your help.


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