首页 > 解决方案 > 使用 @SecondaryTables 但将字段本身放在单独的类中

问题描述

我的实体分布在不同的表中,对我来说最常见的方法是使用 @SecondaryTables。但是我不喜欢使用@SecondaryTables 是第二个/第三个表的字段也必须在同一个Entity Java 类中。因为第二/第三个表有很多字段,将它们放在一起将使它成为一个巨大的类。

有没有一种方法,我可以使用 @SecondaryTables 但将字段物理放置在不同的类中。本来很棒的是有类似的东西

 @SecondaryTables({
    @SecondaryTable(name="Table2", 
        pkJoinColumns=@PrimaryKeyJoinColumn(name="ID_2"), class="someclass")
})

但是 JPA 没有提供这样的工具它只是没有提供 class="someclass" 属性..

那么有什么办法可以做我想做的事吗?

标签: jpa

解决方案


您可以做的最好的事情是使用可嵌入的:

@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;
}

推荐阅读