Difference between @OneToMany and @ElementCollection?
ElementCollection
is a standard JPA annotation, which is now preferred over the proprietary Hibernate annotation CollectionOfElements
.
It means that the collection is not a collection of entities, but a collection of simple types (Strings, etc.) or a collection of embeddable elements (class annotated with @Embeddable
).
It also means that the elements are completely owned by the containing entities: they're modified when the entity is modified, deleted when the entity is deleted, etc. They can't have their own lifecycle.
I believe @ElementCollection
is mainly for mapping non-entities (embeddable or basic) while @OneToMany
is used to map entities. So which one to use depend on what you want to achieve.