-->
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: Mapping a LinkedHashSet
PostPosted: Sat May 07, 2005 5:52 pm 
Newbie

Joined: Fri May 28, 2004 11:08 am
Posts: 5
Is there a way in Hibernate to represent the behavior of a LinkedHashSet?

I have the following situation:
An instance of Class A contains a Set of instances of Class B, but the set must be arbitrarily ordered by the user (i.e: it is not "sorted", but "ordered").
So, I need a mix of List and Set, which is precisely what LinkedHashSet gives.

My DB table to represent the association looks like:
Code:
CLASS_A_ID CLASS_B_ID  ORDER
   A1          B1       0
   A1          B3       1
   A1          B2       2

Please note:
- ORDER column is not represented in either of the two original classes (A and B) since the usage of a LinkedHashSet in A is representative enough.
- There is a Unique constraint on (CLASS_A_ID, CLASS_B_ID), and this is correct since it is a Set: I don't want users to post twice the same B object.

Now, here is the thing:

* If I use a Set, then I will not be able to represent ORDER column.
* If I use a List (and set index column to ORDER) then Hibernate fails to update the table because it breaks the Unique Key (since it relies on non-uniqueness of the List - other posts in the forum talk about that).

What should I do? I know I can create another association class to store this but I really would like to have this issue solved by the persistence framework instead.


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 07, 2005 8:28 pm 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
Are you try make equals method and user simple List

regrads


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 09, 2005 7:30 am 
Newbie

Joined: Fri May 28, 2004 11:08 am
Posts: 5
I'm not sure if that'll work. I am currently using a List, and my equals method is defined correctly in class B, the problem is that when I remove an item from the list, and it's not the last one, Hibernate generates a constraint error when updating the table.

Here is an example:

Suppose that I remove B1 from the list (order 0), then the table should end up having B3 in order 0 and B2 in order 1.
Hibernate performs the following steps:
1) DELETE row with order 2
2) UPDATE row with order 0 to refer to B3
3) UPDATE row with order 1 to refer to B2

The problem is that in step (2) the unique key constraint is violated (because both row with order 0 and that with order 1 refer to B3 in that particular point in time).

You may refer to http://forum.hibernate.org/viewtopic.php?t=932149 for a discussion on this issue.

This is because Hibernate assumes that the list does not contain unique items (i.e: It is not a Set). I am Ok with that, but then using a List does not work for me (unless of course I remove all items and re-add all of them every time I want to delete a single item, but I'd rather search for other alternatives first).

Is there any other way around that I could use to solve my problem?


Top
 Profile  
 
 Post subject: Re: Mapping a LinkedHashSet
PostPosted: Thu Apr 11, 2013 2:08 pm 
Newbie

Joined: Tue Dec 15, 2009 11:56 am
Posts: 2
Hi ,

I am facing exact same problem . Is there any collections supported by hibernate for this purpose ?

Regards,
lalrow


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.