- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有一个相当典型的场景,其中有一个主要的 @Entity 并且他内部的所有内容都是可嵌入的(因此没有父项,其中的所有内容都没有意义)。现在 JPA 2.0 阻止我将 @ElementCollection 嵌套在另一个 @ElementCollection 中定义的 @Embeddable 中:
JSR-317 2.6 Collections of Embeddable Classes and Basic Types An embeddable class (including an embeddable class within another embeddable class) that is contained within an element collection must not contain an element collection, nor may it contain a relationship to an entity other than a many-to-one or one-to-one relationship
现在的问题是:这是为什么?一个简单的例子:
@Entity
public class Tournament {
@Id
Long id;
@ElementCollection
@CollectionTable
private List<Edition>;
}
@Embeddable
public class Edition {
@ElementCollection
@CollectionTable
private List<Round>
}
@Embeddable
public class Round {
blabla;
}
这有什么问题?这只是一个示例,您可以将 Round 和 Edition 定义为 Entity 并解决问题,但在我的例子中,由于多种原因,我需要强制执行没有他的 parent ,非常嵌套的东西没有意义。
为什么 JPA 2.0 必须阻止我这样做?
最佳答案
您的情况违反了您粘贴的规范元素:
Edition 本身就是@Embeddable,并且包含一个Round 的元素集合,因此:
包含在元素集合 (Tournament.editions) 中的可嵌入类 (Edition) 不得包含元素集合 (Edition.rounds)。
至于为什么你不能这样做 - 如果你看一下来自 http://en.wikibooks.org/wiki/Java_Persistence/ElementCollection 的例子然后你会看到 child (版本)将只用一个 FK 映射回所有者(Tournament.id)而没有自己的 ID 列 - 因为作为一个弱实体,它没有自己的 ID自己的,并且仅通过引用锦标赛的 ID 来定义。
以 Round 为例,如果它也是一个弱实体,那么它应该由 FK 引用版本定义 - 但我们已经说过它没有自己的 ID,所以你不能在没有它的情况下将它映射到数据库中向 Edition 添加一个 ID - 此时它本身就是一个实体,而不仅仅是 @Embeddable。
查看下面评论中的维基百科示例 - http://en.wikipedia.org/wiki/Weak_entity - 弱实体的例子有 OrderNumber、CustomerNumber 等 - 只有嵌入到另一个对象中才有意义的东西。
您仍然可以拥有具有父映射(即版本上的锦标赛引用)和/或双向引用的实体。您可以使用 @ManyToOne 注释上的 nullable=false 属性强制在 Edition 上定义父级,从而强制执行模型的要求。
关于java - 可嵌入和 ElementCollection 嵌套,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22126397/
public enum ReportStatus { SUCCCEED, FAILED; } public class Work { @ElementCollection @E
如何使用 JPQL 通过 ElementCollections 查询 JPA 实体,其中 ElementCollection 包含给定元素集中的所有元素? 例如,如果 Node 实体定义了“属性”的
我的目标是克隆实体“产品”及其所有过滤器。 例如,我有一个实体(为简单起见,省略了 getter 和 setter): @Entity public class Product { @Id
我有一个这样声明的“标签”属性: @Entity public class BlogArticle { [...] @ElementCollection(fetch = FetchTy
我有 JavaEE 应用程序并使用 Hibernate 4.3.7.Final。在我的数据库中有两个表:RECHT(包含列:RECHT_RECHTEART(VARCHAR)和GEOB_ID(NUMBE
我想减少 spring 运行的查询量。当通过 SQL 获取带有 @ElementCollection 的对象时,我想直接通过查询中的 JOIN 获取 ElementCollections 的数据。 具
我使用 Hibernate 3.5.6 作为我的 JPA 2.0 实现。我正在尝试在我的实体中构建一个 @ElementCollection(省略了许多字段): @Entity public clas
我有一个包含元素集合的实体类 PositionOrdering: @ElementCollection(targetClass = Position.class, fetch = FetchType.
我有一个多对多关系,它为项目分配标签(除了字符串之外什么都没有): +------+ +------------+ +-------+ | ITEM | | ITEM_LABEL |
我有一个 @Entity命名 Video ,它包含 @ElementCollection标签: @Entity @Table(name = "videos") public class Video {
我试图弄清楚如何使用枚举列表(@ElementCollection)对实体进行 DTO 投影。不幸的是,缺少 QueryDsl 文档,在这里我只能找到版本 3 的结果 不是 适用于版本 4。 @Ent
我有一个具有不同字段的实体: @Entity public class TestEntity { private int id; private String name; pr
给定一个实体产品 @Entity public class Product { private Long id; private List reviews; public Pr
我的数据库中有两个表,如下所示,外键来自 job_label.job_id到 job_record 中的等效列。此外,job_id 的三元组, label_key ,和label在job_record
我有一个具有不同字段的实体: @Entity public class TestEntity { private int id; private String name; pr
我有一个 @ElementCollection @CollectionTable( name = "orgattributes", joinColumns = @JoinColumn(name =
我有一个 User 类,可以有多个登录名: @Entity public class User { @ElementCollection private List logins = new A
对于这个问题,请考虑以下示例: @Entity public class File { public static enum Permission { READABLE, WRITEABLE,
无论如何,我可以在没有这个注释的情况下获得 @ElementCollection 的效果吗?我使用的是 Hibernate 3.3,而 @ElementCollection 和 @Collection
我有以下实体,对此示例进行了简化: @Entity public class Subscription { @Column private String user; @Column
我是一名优秀的程序员,十分优秀!