JPA 一个实体有两个 table 通过 SecundaryTable

JPA One Entity with two table via SecundaryTable

我无法使用以下简化设置。我只想获得一个 class 中表的两个值,条件为 API。我不想将两个 classes 与 @OneToOne 或 @OneToMany 等一起使用

这可能吗? JPA 不是这样的吗?

第一个Table:

test.table_1(
  id integer,
  value_1 text,
  table_2_id integer
)

第二个Table:

test.table_2(
  id integer,
  value_2 text
)

实体Class:

@Entity
@Table(schema = "test", name = "table_1")
@SecondaryTable(schema = "test", name = "table_2", pkJoinColumns = {
    @PrimaryKeyJoinColumn(name = "id", referencedColumnName = "table_2_id")
})
public class JointEntity {
  @Id
  private Integer id;
  @Column(name = "value_1")
  private String value1;
  @Column(name = "value_2", table = "table_2")
  private String value2;

  // getter and setter
}

执行时抛出异常:

Exception in thread "main" org.hibernate.cfg.RecoverableException: Unable to find column with logical name: table_2_id in org.hibernate.mapping.Table(test.table_1) and its related supertables and secondary tables
    at org.hibernate.cfg.Ejb3JoinColumn.checkReferencedColumnsType(Ejb3JoinColumn.java:831)
    at org.hibernate.cfg.annotations.TableBinder.bindFk(TableBinder.java:608)
    at org.hibernate.cfg.annotations.EntityBinder.bindJoinToPersistentClass(EntityBinder.java:794)
    at org.hibernate.cfg.annotations.EntityBinder.createPrimaryColumnsToSecondaryTable(EntityBinder.java:786)
    at org.hibernate.cfg.annotations.EntityBinder.finalSecondaryTableBinding(EntityBinder.java:714)
    at org.hibernate.cfg.SecondaryTableSecondPass.doSecondPass(SecondaryTableSecondPass.java:29)
    at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1621)
    at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1586)
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:278)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:858)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:885)
    at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:58)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
    at de.icybits.test.HibernatePerformanceTest.main(HibernatePerformanceTest.java:18)
Caused by: org.hibernate.MappingException: Unable to find column with logical name: table_2_id in org.hibernate.mapping.Table(test.table_1) and its related supertables and secondary tables
    at org.hibernate.cfg.Ejb3JoinColumn.checkReferencedColumnsType(Ejb3JoinColumn.java:826)
    ... 14 more

就像 Billy Frost 在他的推荐@Secundary 中提到的那样Table 不打算以这种方式使用。所以我做了进一步的研究。因为 Billy Frost 的评论没有完全回答我的问题。

  1. 是否可以存档上述设置(1 个实体中的 2 Table)?

答案是肯定的我在另一个问题中找到了解决方法

  1. 这样的事情不是 JPA 设计的吗?

正如我在 1 中提到的那样,这只是一种解决方法。一般来说,它不打算只是将一个单一的外列关联到一个实体。 (我不是 100% 确定这一点,但经过进一步研究,这就是我的结论。)

最后,我要感谢 Billy Frost 的快速评论。因此,我从我的研究中排除了@SecundaryTable,并且可以找到更好的答案。