JPA:何时选择多值关联与元素集合映射 [英] JPA: When to choose Multivalued Association vs. Element Collection Mapping

查看:160
本文介绍了JPA:何时选择多值关联与元素集合映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更好地理解

(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 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

这篇关于JPA:何时选择多值关联与元素集合映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆