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.

Leave a Comment