Difference Between @Onetomany and @Elementcollection

Difference between @OneToMany and @ElementCollection?

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.

JPA: When to choose Multivalued Association vs. Element Collection Mapping

Intuitively, I would typically use the @ElementCollection for composition scenarios. But even that feels very similar like CascadeType=DELETE

They are similar, with some slight differences. The ElementCollection page from the Java Persistence wikibook summarizes it pretty well:

Emdedded Collections


An ElementCollection mapping can be
used to define a collection of
Embeddable objects. This is not a
typical usage of Embeddable objects as
the objects are not embedded in the
source object's table, but stored in a
separate collection table. This is
similar to a OneToMany, except the
target object is an Embeddable instead
of an Entity. This allows collections
of simple objects to be easily
defined, without requiring the simple
objects to define an Id or ManyToOne
inverse mapping. ElementCollection can
also override the mappings, or table
for their collection, so you can have
multiple entities reference the same
Embeddable class, but have each store
their dependent objects in a separate
table.

The limitations of using an
ElementCollection instead of a
OneToMany is that the target objects
cannot be queried, persisted, merged
independently of their parent object.
They are strictly privately-owned
(dependent) objects, the same as an
Embedded mapping. Their is no cascade
option on an ElementCollection, the
target objects are always persisted,
merged, removed with their parent.
ElementCollection still can use a
fetch type and defaults to LAZY the
same as other collection mappings.

See also

  • Embeddables (Aggregates, Composite or Component Objects)

Use of @OneToMany or @ManyToMany targeting an unmapped class for ListString in Spring data jpa

Hibernate supports three data mapping types: basic (e.g String, int), Embeddable and Entity. Most often, a database row is mapped to an Entity, each database column being associated to a basic attribute. Embeddable types are more common when combining several field mappings into a reusable group (the Embeddable being merged into the owning Entity mapping structure).

Both basic types and Embeddables can be associated to an Entity through the @ElementCollection, in a one-Entity-many-non-Entity relationship.

  @ElementCollection
@CollectionTable(name="photoUrls",
joinColumns = @JoinColumn(name = "user_id"))
@Valid
private List<String> photoUrls = new ArrayList<String>();

So you need to remove @OneToMany as OneToMany is for one-Entity-many-Entity relationship. If you want to map to an Entity, then need change the code like below and create a new Entity Photo.

@OneToMany(
cascade = CascadeType.ALL,
orphanRemoval = true
)
private List<Photo> photos = new ArrayList<>();

In the above code , Photo is an Entity.

see the below link for more information:
https://vladmihalcea.com/how-to-optimize-unidirectional-collections-with-jpa-and-hibernate/

@ElementCollection or @OnetoMany with Entity or primitives?

I personally always prefer to use a one to many relationship to other entities when doing something like this. My reason is that it is a much more flexible design going forward. It is pretty easy to see a scenario where instead of just knowing the column name you will want additional information. By using a real entity instead of a string you simply add a new property to your class. If you are using just a string though you will have a much more extensive refactoring on your hands. Takes a little longer in the beginning but it almost always pays off in the end.

Hibernate @One-To-Many similar to @ElementCollection

Yes, by specifying that the OneToMany must use a join column instead of the default (which uses a join table):

@OneToMany(...)
@JoinColumn(name = "BOOK_ID")
private Set<BookTag> tags;


Related Topics



Leave a reply



Submit