I have four entities to map together, "Association", "Account", "Transaction" and "TransactionEvent". The id of Association is a simple integer id. Account and Transaction each have embedded id's consisting of a mapping to an Association and a number.
TransactionEvent should have an embedded id consisting of one Account and one Association. Now, each of those are mapped to an Association, and I want it to be the same Association for one TransactionEvent.
JPA Annotations is used for the Hibernate mapping, but I cannot make this work. I have tried forcing the same column name for the Association key, but Hibernate complains about repeated columns.
Is this possible to solve, or am I not thinking straight?
Here are the annotated classes, but I trimmed away getters/setters and non-id columns, annotations from the javax.persistence namespace:
@Entity
public class Association implements Serializable {
@Id @GeneratedValue(strategy = GenerationType.AUTO)
private long id;
}
@Embeddable
public class AccountPK implements Serializable {
@ManyToOne(optional=false)
private Association association;
@Column(nullable=false)
private int number;
}
@Embeddable
public class TransactionPK implements Serializable {
@ManyToOne
private Association association;
@GeneratedValue(strategy=GenerationType.AUTO)
private long number;
}
@Embeddable
public class AccountEventPK implements Serializable {
@ManyToOne(optional=false)
@JoinColumns({
@JoinColumn(name="association_id", referencedColumnName="association_id"),
@JoinColumn(name="account_number", referencedColumnName="number")
})
private Account account;
@ManyToOne(optional=false)
@JoinColumns({
@JoinColumn(name="association_id", referencedColumnName="association_id"),
@JoinColumn(name="transaction_number", referencedColumnName="number")
})
private Transaction transaction;
}
Actual Account, Transaction and AccountEvent entities are on the form
@Entity
public class Account implements Serializable {
@EmbeddedId
private AccountPK id;
}