Quick revision
The short answer
The rows of an @ElementCollection have no id, so when a List without an order column changes, Hibernate cannot tell which row to update and simply deletes every row for the owner and inserts the collection again. A Set or an @OrderColumn lets it target rows more precisely, but some changes still cause many statements. For large or frequently changing collections we should map the phone number as an entity with its own id; @ElementCollection suits small value lists that rarely change.
What to remember
In this answer, we saw that element collection rows have no identity, so for a bag Hibernate deletes all rows of the owner and inserts them again on any change. A Set lets it delete by matching all columns and an @OrderColumn lets it update by index, but both still cost many statements for some changes. For large or busy collections we map an entity with its own id, and we keep @ElementCollection for small, rarely changed value lists.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The rows of an @ElementCollection have no id, so when a List without an order column changes, Hibernate cannot tell which row to update and simply deletes every row for the owner and inserts the collection again. A Set or an @OrderColumn lets it target rows more precisely, but some changes still cause many statements. For large or frequently changing collections we should map the phone number as an entity with its own id; @ElementCollection suits small value lists that rarely change.
Why this question is asked
This problem usually shows up in the SQL log long after the mapping was written: one phone number changes, and we see a delete of all of a customer's phone numbers followed by five inserts. On a busy table this means extra write load, more locking and noisy audit triggers. Interviewers ask it because the cause, collection semantics and missing identity, explains a whole family of Hibernate behaviours. In this answer, we will recap what an element collection is, reproduce the rewrite, see why it happens, look at how a Set or an order column changes it, and then decide when to replace it with an entity.
1A Quick Recap of @ElementCollection
@ElementCollection maps a collection of values, either basic types like String or embeddables like PhoneNumber, into a separate table that holds a foreign key to the owner. Unlike a @OneToMany, the elements are not entities: they have no id, no repository and no life of their own. They are loaded, saved and deleted only through their owner.
So the table for our phones has columns such as customer_id, type and number, and nothing else. That missing id column is the whole story of this question.
2Reproducing the Rewrite
Here is the mapping with a List and no order column, which Hibernate treats as a bag, an unordered collection that may contain duplicates.
One changed element produced six statements. With 200 elements it would be 201, and the delete takes row locks on all of them.
3Why Hibernate Rewrites the Whole Collection
To update one row, Hibernate needs a way to say which row in a WHERE clause. An entity has its id for that. An element in a bag has nothing: there is no id, there is no position column, and the bag may even contain two identical elements, so matching on all columns could hit the wrong row or two rows at once.
Faced with that, the only safe strategy is to delete everything that belongs to the owner and insert the current state. It is correct, simple and always works. The reason it feels wrong is that we picture the rows as individual records, while Hibernate, following the mapping we gave it, sees them as one value: the collection as a whole.
- Does each row have an id?entities do, elements do notYesUpdate or delete that rowNoLook for another key
- Is there an order column?
@OrderColumnon a ListYesUpdate by owner and indexNoCheck for a Set - Is it a Set of unique values?YesDelete by matching all columnsNoBag: delete all, insert all
3 steps in order. Play them, or step through them one at a time.
Without an id, the more structure the collection has, the more precisely Hibernate can write.
4How a Set or an Order Column Helps, and Where It Stops
If we change the type to Set<PhoneNumber> and give PhoneNumber proper equals and hashCode, the elements are unique. Hibernate can then delete one element with a statement that matches every column, where customer_id = ? and type = ? and number = ?, and insert the new one. Two statements instead of six. But there is a catch: if any of those columns can be null, the match with = fails for null values, and a Set still needs the whole collection loaded to check uniqueness.
If we keep a List and add @OrderColumn(name = "position"), every row has a position, and Hibernate can update the row at a given index. Changing element 2 becomes one update. But removing element 0 shifts every later element down one place, so Hibernate updates the position or values of all later rows and deletes the last one. For lists where people insert or remove at the front, that is nearly as expensive as the full rewrite.
So both help with some changes and neither makes every change cheap. They reduce the rewrites; they do not give the rows a real identity.
5When to Map an Entity Instead
When the collection can grow large, changes often, is edited one item at a time, or needs to be referenced from elsewhere, the elements are telling us they have identity after all. Then the right model is a small entity with its own id and a @OneToMany from the customer, or even a unidirectional @ManyToOne from the phone to the customer.
Now each phone number is updated by id, can be loaded alone, can have a version column for optimistic locking, and can be queried directly. The cost is a little more code: a repository or cascade settings, and the parent-child bookkeeping of a bidirectional association if we keep one.
So my rule is simple. @ElementCollection is a good fit for small value lists that rarely change and are always read with their owner, such as a handful of tags, the languages a user speaks, or the allowed currencies of an account. Once a collection has dozens of elements or changes item by item, it should be an entity.
6Follow-up questions
Does the rewrite happen when we only add an element to the end of a bag?
Hibernate can often treat a pure append to a bag as inserts only, because nothing existing changed. Any update or removal, or certain ways of replacing the collection, brings back the delete-all and insert-all behaviour. Rather than rely on these details, check the SQL log for the operations your code actually performs.
Why is replacing the whole collection with a new list especially bad?
If we call setPhones(newList), Hibernate sees that the collection instance it was tracking has been dereferenced and must treat the new one as a complete replacement. That forces a full delete and re-insert even for a Set or an ordered List. It is better to modify the managed collection in place with clear, add and remove.
Can element collections cause other performance problems?
Yes. Loading several entities with element collections in one query has the same N+1 and Cartesian product risks as @OneToMany, and an eager element collection is loaded with every owner. Element collections can also not be queried or paged as independently as entities can, which matters once they grow.
7Conclusion
In this answer, we saw that element collection rows have no identity, so for a bag Hibernate deletes all rows of the owner and inserts them again on any change. A Set lets it delete by matching all columns and an @OrderColumn lets it update by index, but both still cost many statements for some changes. For large or busy collections we map an entity with its own id, and we keep @ElementCollection for small, rarely changed value lists.
Open with
The cause is that element collection rows have no identity. The customer_phone table has only the customer id and the phone columns, so when a List without an order column changes, Hibernate treats it as a bag that may even contain duplicates and has no reliable way to target one row.
Then, if they want more
The safe strategy is to delete all the customer's rows and insert the current collection, which is what we see in the log. We can reduce that. With a Set and proper equals and hashCode on PhoneNumber, Hibernate can delete one element by matching every column, although nullable columns break that match.
With @OrderColumn on a List it can update a row by index, but removing from the front shifts all later positions, so it is still many statements. Neither removes the rewrites for every change. I would also check that the code does not replace the collection with a new list, which forces a full rewrite whatever the type.
My rule is that @ElementCollection is fine for small value lists that rarely change and are always read with their owner, like a few tags or languages.
If the phone numbers can be many, change one at a time, or need to be referenced, I would map Phone as an entity with its own id, so updating one number is one update by id.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Data JPA reference, Hibernate ORM documentation, Spring Framework reference, Transaction management
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "An entity has @ElementCollection List<PhoneNumber> phones. Every update to one phone...": https://scrivio-interview-library.netlify.app/databases/jpa-relationships/649288251b/