Lets say I have these tables:
Table A has a composite primary key on two fields:
- Employee ID (String)
- Transaction ID (Long)
Table B also has a composite primary key, but on three fields:
- Employee ID (String)
- Transaction ID (Long)
- Date (Date)
In table B, there is a Foreign Key on Employee ID and Transaction ID called "FK1" (for simplicity).
Since the composite id used in table A is used across several mapped hibernate classes, I create a class I can reuse:
@Embeddable
public class EmployeeTransactionComposite {
private String employeeId;
private Long transactionId;
}
So, the class mapped to Table A looks like this:
public ClassA implements Serializable {
private EmployeeTransactionComposite id;
// rest of fields
@EmbeddedId
public EmployeeTransactionComposite getId() {
return id;
}
}
The class mapped to table B looks like this:
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;
}
}
}
Obviously I wouldn't be posting this if it was working. It blows up with a stacktrace like this:
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
This is a mapping to a legacy schema, so alterations are not possible. Can anyone help?