Hibernate: @ElementCollection for a list of values

A second table for simple values, without making them entities.

Code
@Entity
class Order {
    @Id Long id;

    @ElementCollection
    @CollectionTable(name = "order_tags", joinColumns = @JoinColumn(name = "order_id"))
    @Column(name = "tag")
    Set<String> tags = new HashSet<>();
}
Output
order_tags(order_id, tag)
The rows are owned entirely by the order and deleted with it.
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