使用 JPA/Hibernate 3.6/DB2。
我有以下异常: 原因:org.hibernate.AnnotationException:SecondaryTable JoinColumn cannot reference a non primary key
造成的:
public class XRequest {
@ManyToOne
@JoinTable(
name = "RequestBatch",
joinColumns = @JoinColumn(name = "requestBatchID", referencedColumnName="requestBatchID"),
inverseJoinColumns = @JoinColumn(name = "requestVersionID")
)
private Requestversion requestversion;
}
requestBatchID 不是主键,而是从 RequestBatch 表中导入的键(在那里,它确实是主键)。为什么 JoinTable 必须使用主键?我的意思是,我不是刚刚定义这是一个多对一关联吗?为什么它必须是主键?
指定:这是表格的样子。
XRequest (
requestId int (primary)
requestBatchId int (imported key from RequestBatch)
)
RequestBatch (
requestBatchId int (primary)
requestVersionId int
)
RequestVersion (
requestVersionId int (primary)
)
想要的结果是 Hibernate 为我构建的这个 SQL 查询:
select xr, rv
from XRequest xr
left outer join RequestBatch rb on rb.requestBatchId = xr.requestBatchId
inner join RequestVersion rv on rb.requestVersionId = rv.requestVersionId