I have an entity with two @ElementCollection columns:
@Entity
public class User1 {
@Id
@GeneratedValue
private int id;
@ElementCollection
private Set<String> stringsSet = new HashSet<>();
@ElementCollection
private List<String> stringsList = new ArrayList<>();
...
}
When I remove one element from the stringsSet, hibernate generates one “delete” command:
delete
from
User1_stringsSet
where
User1_id=?
and stringsSet=?
When I remove one element from the stringsList, hibernate generates “delete” that removes all rows and “insert” for each remaining element in the collection:
delete
from
User1_stringsList
where
User1_id=?
insert
into
User1_stringsList
(User1_id, stringsList)
values
(?, ?)
insert
into
User1_stringsList
(User1_id, stringsList)
values
(?, ?)
... etc (same insert for each remain value in stringsList)
From these two examples we can see that the generated code for the stringsSet is much more efficient.
Question: why does hibernate generate different and inefficient code for the stringsList?