假设我有这些表:
表 A 在两个字段上有一个复合主键:
- 员工 ID(字符串)
- 交易 ID(长)
表 B 也有一个复合主键,但在三个字段上:
- 员工 ID(字符串)
- 交易 ID(长)
- 日期(日期)
在表 B 中,Employee ID 和 Transaction ID 上有一个外键,称为“FK1”(为简单起见)。
由于表 A 中使用的复合 id 用于多个映射的休眠类,因此我创建了一个可以重用的类:
@Embeddable
public class EmployeeTransactionComposite {
private String employeeId;
private Long transactionId;
}
因此,映射到表 A 的类如下所示:
public ClassA implements Serializable {
private EmployeeTransactionComposite id;
// rest of fields
@EmbeddedId
public EmployeeTransactionComposite getId() {
return id;
}
}
映射到表 B 的类如下所示:
public ClassB implements Serializable {
private ClassBCompositeId id;
// fields
@EmbeddedId
public getId() {
return this.id;
}
@Embeddable
public class ClassBCompositeId implements Serializable {
private EmployeeTransactionComposite composite;
private Date date;
@ManyToOne
@ForeignKey(name="FK1")
public EmployeeTransactionComposite getComposite() {
return composite;
}
@Column(name="THEDATE")
public Date getDate() {
return date;
}
}
}
显然,如果它有效,我不会发布这个。它会出现这样的堆栈跟踪:
Caused By: java.lang.NullPointerException
at org.hibernate.cfg.Configuration.processFkSecondPassInOrder(Configuration.java:1419)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1359)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1728)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1779)
at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:189)
Truncated. see log file for complete stacktrace
这是到旧模式的映射,因此不可能进行更改。任何人都可以帮忙吗?