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.