0

In my parent class I have this relation

@ElementCollection
@CollectionTable(name = "PARENT_MYENUMS", joinColumns = @JoinColumn(name = "parent_id"))
@Column(name = "MYENUM")
@Enumerated(EnumType.STRING)
private Set<MyEnum> myenums = new HashSet<>();

Where MyEnum is like that

public enum MyEnum {
    A,
    B
}

To add to my parents I use something like that

parent.getMyEnums().add(MyEnum.A);

But if I save the parent for multiple time when I have to do an update, the save multiply the data from collection table PARENT_MYENUMS

Any advice to prevent this multiple inserts in collection table?

Adrian Ispas
  • 152
  • 1
  • 5
  • 2
    Does this answer your question? [Hibernate - @ElementCollection - Strange delete/insert behavior](https://stackoverflow.com/questions/3742897/hibernate-elementcollection-strange-delete-insert-behavior) - The behaviour you see is because hibernate cannot create a PK for the table `PARENT_MYENUMS` so each update on the collection results on a delete of the old associated values + an insert for each value in the Enum. I don't think there's a solution for a Set+@ElementCollection, so you might need to create an embeddable wrapper around the enum, but cannot say if that would work. – Augusto Nov 04 '19 at 17:12
  • can you share where are you doing save or update. – saifulislampi Nov 05 '19 at 09:13
  • also, use list if you absolutely doesn't need set – saifulislampi Nov 05 '19 at 10:26
  • @saifulislamplabon I do an update I need a set because `MyEnum` have to be maximum just one of each type. – Adrian Ispas Nov 05 '19 at 11:45
  • 1
    Ensure that in some other way, maybe prepersist ot pre update method or manually cleaning up duplicates. Because set doesn't play nice with jpa – saifulislampi Nov 05 '19 at 11:48

0 Answers0