jpa - 使用 @SecondaryTables 但将字段本身放在单独的类中
问题描述
我的实体分布在不同的表中,对我来说最常见的方法是使用 @SecondaryTables。但是我不喜欢使用@SecondaryTables 是第二个/第三个表的字段也必须在同一个Entity Java 类中。因为第二/第三个表有很多字段,将它们放在一起将使它成为一个巨大的类。
有没有一种方法,我可以使用 @SecondaryTables 但将字段物理放置在不同的类中。本来很棒的是有类似的东西
@SecondaryTables({
@SecondaryTable(name="Table2",
pkJoinColumns=@PrimaryKeyJoinColumn(name="ID_2"), class="someclass")
})
但是 JPA 没有提供这样的工具它只是没有提供 class="someclass" 属性..
那么有什么办法可以做我想做的事吗?
解决方案
您可以做的最好的事情是使用可嵌入的:
@Entity
public class SomeEntity {
@Embedded
private SomeOtherClass someOtherClass; //mappings for table 2
}
@Embeddable
public class SomeOtherClass {
// define columns from table 2
@Column(table = "Table2")
public String someColumn;
}
推荐阅读
- java - 如何检查一个元素是否存在于另一个Arraylist中
- r - 求加权比例的标准误(调查数据)
- python - 在计算中使用大数时,Python Numba 模块给了我一个错误的答案
- java - 从 JSON 响应中检索所需的 JSON 对象
- python-3.x - 执行时 tkinter GUI 出错 --invalid command name "2025099205632update_values"
- css - 是否可以将 CSS 与 SharePoint Online 一起使用?
- python - 使用 Selenium 提取数据 - 如何定位元素?
- r - as.factor 无法将数值变量转换为分类变量
- r - 自动识别变量列中包含的“日期”一词并将条目更改为日期格式的功能(尽管存在异质性)
- javascript - 可访问性 - 分组复选框,其中一个复选框具有相关的文本区域