通过SecundaryTable具有两个表的JPA一个实体

时间:2018-11-14 11:19:22

标签: hibernate jpa criteria jpa-annotations

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

这可能吗? JPA不需要这样的东西吗?

第一张桌子:

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

第二张表:

test.table_2(
  id integer,
  value_2 text
)

实体类:

@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

1 个答案:

答案 0 :(得分:0)

在他赞扬@SecundaryTable中提到的Billy Frost时,不打算以此方式使用它。所以我做了进一步的研究。因为Billy Frost的评论未完全回答我的问题。

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

答案是肯定的,我在另一个问题Hibernate - Join only single column instead of whole entity

中找到了解决方法
  1. JPA不想使用这种东西吗?

正如我在1.中提到的,这只是一种解决方法。通常,不打算仅将单个外部列关联到实体。 (我对此不是100%肯定,但是经过进一步研究,这就是我得出的结论。)

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