JPA:何时选择多值关联与元素集合映射 [英] JPA: When to choose Multivalued Association vs. Element Collection Mapping
问题描述
我想更好地理解
(1)传统的多值关系/关联
@Entity - > @OneToMany - > @Entity
和
<2>嵌入式(和基本)类型的JPA2 集合
@Entity - > @ElementCollection - > @Embeddable
我看到了语法上的差异,但不知道是否也存在性能影响 EM>。在底层,数据库实现看起来非常相似。
直觉上,我通常对组合场景使用 @ElementCollection
。但即使是这样的感觉非常相似,如 CascadeType = DELETE
。
我在这里错过了本质吗?
谢谢J。
直觉上,我通常会使用@ElementCollection编写组合场景。但即使如此,CascadeType = DELETE
也非常相似,它们是相似的,只是略有不同。 ElementCollection 页面/ wiki / Java_Persistencerel =noreferrer> Java Persistence wikibook总结得非常好:
Emdedded Collections
ElementCollection
映射可以是
,用于定义
Embeddable
对象的集合。这不是
典型的使用可嵌入的
对象作为
对象不是嵌入在
源文件中的 对象的表,但存储在
分离的集合表中。这是
类似于OneToMany
,除了
目标对象是Embeddable
,而不是
是实体
。这允许简单对象的集合
很容易被定义为
,而不需要简单的
对象来定义Id
或ManyToOne
逆映射。ElementCollection
can
也可以覆盖它们的集合的映射或表
,因此您可以让
多个实体引用相同的
可嵌入
类,但每个存储
其依赖对象在单独的
表中。
使用
ElementCollection
而不是
OneToMany
的限制是目标对象
不能被查询,持久化,独立于其父对象合并
。
它们是严格私有的
(依赖)对象,与
嵌入
映射相同。他们在ElementCollection
上没有cascade
选项,
目标对象总是被保留下来,
合并,与父母一起移除。
ElementCollection
仍然可以使用
提取类型,默认为LAZY
另请参阅
I would like to better understand the differences between
(1) a traditional Multivalued Relationship/Association
@Entity -> @OneToMany -> @Entity
and
(2) the JPA2 Collection of Embeddable (and basic) Types
@Entity -> @ElementCollection -> @Embeddable
I see the syntactical differences, but wonder whether there are also performance implications. Under the hood, the database implementation looks very similar.
Intuitively, I would typically use the @ElementCollection
for composition scenarios. But even that feels very similar like CascadeType=DELETE
.
Am I missing the essence here? Is one more efficient than the other for certain purposes?
Thank you, J.
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 ofEmbeddable
objects. This is not a typical usage ofEmbeddable
objects as the objects are not embedded in the source object's table, but stored in a separate collection table. This is similar to aOneToMany
, except the target object is anEmbeddable
instead of anEntity
. This allows collections of simple objects to be easily defined, without requiring the simple objects to define anId
orManyToOne
inverse mapping.ElementCollection
can also override the mappings, or table for their collection, so you can have multiple entities reference the sameEmbeddable
class, but have each store their dependent objects in a separate table.The limitations of using an
ElementCollection
instead of aOneToMany
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 anEmbedded
mapping. Their is nocascade
option on anElementCollection
, the target objects are always persisted, merged, removed with their parent.ElementCollection
still can use a fetch type and defaults toLAZY
the same as other collection mappings.
See also
这篇关于JPA:何时选择多值关联与元素集合映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!