gpt4 book ai didi

java - 结合 @MappedSuperclass 和 @SecondaryTable - 允许吗?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:14:29 31 4
gpt4 key购买 nike

为我的实习工作开发 JPA 2.0 合规性工具包。该套件的一部分用于覆盖角落案例。

@MappedSuperclass 没有原始表,因为它不是实体。JSR-317 声明了这个有趣的位:

A class designated with the MappedSuperclass annotation can be mapped in the same way as an entity except that the mappings will apply only to its subclasses since no table exists for the mapped superclass itself.

“因为映射父类(super class)本身不存在表。”

这是否意味着“无默认值”- 如果您愿意,您可以制作自己的表格,但它不能自行检索?

或者这是否意味着“映射的父类(super class)不能存在任何表”——您不能创建一个(使用 @SecondaryTable)?

根据 JPA 2.0,是否允许将 @MappedSuperclass 的一部分映射到 @SecondaryTable

最佳答案

我不确定你的意思是在映射的父类(super class)中使用@SecondaryTable-annotation 还是在映射的父类(super class)中定义的属性持久化到派生映射的实体中的@SecondaryTable 表,所以我尝试回答这两个问题。

映射的父类(super class)没有自己的表。上下文被映射的父类(super class)用于定义表。这是有道理的,因为否则从一个映射父类(super class)继承到多个实体的映射总是会产生需要覆盖除一个派生实体之外的所有其他实体中的表名称。我认为“映射父类(super class)不存在表”也关闭了@SecondaryTable 的使用出。

此外,根据 JPA 2.0 规范第 12.3 章给出的 XML 模式不允许映射父类(super class)的二级表。

但是在实体中使用@AttributeOverride 的列元素将派生自映射父类(super class)的属性持久化到实体的辅助表应该没问题。规范中没有任何内容反对这种情况。

所以我认为“映射的父类(super class)不能存在任何表”是正确的,但是没有什么能阻止您将这些属性放置到实体的辅助表中。关于列的表的决定发生在派生属性和关联映射的实体中,而不是在映射的父类(super class)本身中。

关于java - 结合 @MappedSuperclass 和 @SecondaryTable - 允许吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9095111/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com