我有一个与此非常相似的问题:如何在辅助表中的非主键列上连接表? 但我不确定我是否可以应用相同的解决方案。
我有两个这样的表:
CREATE TABLE CUSTOMER
(
CUSTOMER_ID INTEGER NOT NULL,
DETAIL_ID INTEGER NOT NULL,
PRIMARY KEY( CUSTOMER_ID ),
CONSTRAINT cust_fk FOREIGN KEY( DETAIL_ID ) REFERENCES DETAILS( DETAIL_ID )
)
CREATE TABLE DETAILS
(
DETAIL_ID INTEGER NOT NULL,
OTHER INTEGER NOT NULL,
PRIMARY KEY( DETAIL_ID )
)
我想将这些表映射到一个名为 的类Customer
,所以我有:
@Entity
@Table(name = "CUSTOMERS")
@SecondaryTable(name = "DETAILS", pkJoinColumns=@PrimaryKeyJoinColumn(name="DETAIL_ID"))
public class Customer {
@Id
@GeneratedValue
@Column(name = "CUSTOMER_ID")
private Integer id;
@Column(table = "DETAILS", name = "OTHER")
private Integer notes;
// ...
}
但这仅在主表中DETAIL_ID
匹配时才有效。CUSTOMER_ID
所以我的问题是:如何使用主表中的外键字段来加入辅助表的主键?
更新 我试图设置:
@SecondaryTable(name = "DETAILS", pkJoinColumns=@PrimaryKeyJoinColumn(name="DETAIL_ID", referencedColumnName="DETAIL_ID"))
但是当我运行应用程序时,我得到了这个异常:
org.hibernate.MappingException: Unable to find column with logical name: DETAIL_ID in org.hibernate.mapping.Table(CUSTOMERS) and its related supertables and secondary tables