JPA: Unique Collection "OneToMany"

I want to display a "collection". A collection is a group of items that the user can group as desired.

@Entity 
class Item {
  Long id;
  String name
}

@Entity 
class MyCollection {
  Long id;
  String name;

  @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
  Set<Item> items;
}

      

The user must be able to put the Item

name "foo" in the "MyCollection" the name "bar" and in the "MyCollection" the name "bar2"

The junction table JPA / Hibernate generated for me MyCollection_Item

has 4 indexes, PK ( MyCollection_id, Item_id

), unique index ( Item_id

) and 2 foreign keys.

I don't want it to create a unique index on Item_id

. This is preventing me:

MyCollection_id, Item_id

1, 1

1, 2

2, 1

I'm sure this type of display should be possible? Any help would be greatly appreciated.

+3


source to share


2 answers


OneToMany is when you want to map one object to many objects



ManyToMany - this is when you want to compare object many objects many (for example, you are trying to do here)

+2


source


You need a ManyToMany, but you've rendered it as OneToMany.



+1


source







All Articles