Hibernate: Set beats List for a @OneToMany you modify

A List with a join table is deleted and reinserted wholesale on any change; a Set updates the row that changed.

Code
// Deletes all rows and reinserts on every change:
@OneToMany @JoinTable(...)
List<Tag> tags;

// Updates just what changed:
@OneToMany @JoinTable(...)
Set<Tag> tags = new HashSet<>();
Output
List: delete from order_tags where order_id=?;  then 20 inserts
Set:  insert into order_tags values (?, ?)      just the new one
Advertisement

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-08-11