JPA Composite primary key issue
Hi,
I have the following JPA Entities:
Employee
EmployeePasswordHistory (The obvious purpose of this entity is to maintain a history of passwords for a user to prevent reusage)
EmployeePasswordHistoryPK
The Employee entity contains a list of EmployeePasswordHistory objects like this:
@OneToMany(mappedBy="employeeId")
private List<EmployeePasswordHistory> passwordHistory = new ArrayList<EmployeePasswordHistory>();
In EmployeePasswordHistory, I have an @Embedded primary key object like this:
@EmbeddedId
private EmployeePasswordHistoryPK pk = new EmployeePasswordHistoryPK();
and a reference to the Employee entity, like this;
@ManyToOne
@JoinColumn(name="EMPLOYEEID", insertable=false, updatable=false)
private Employee employeeId;
In EmployeePasswordHistoryPK, I have the following four primary key fields:
@Basic
@Column(name="employeeID", insertable=false, updatable=false)
private Long employeeid2;
@Basic
@Column(name="sitedirectoryID")
private Long siteDirectoryId;
@Basic
@Column(name="employeePassword")
private String hashedPassword;
@Temporal(TemporalType.TIMESTAMP)
@Column(name="passwordChangeDate")
private Calendar lastChangedDate = Calendar.getInstance();
These four primary key field values are taken from the Employee object.
The odd thing is that when I first create an Employee, an entry in the EmployeePasswordHistory table automatically gets created without me having to add anything in the EmployeePasswordHistory
list mentionned above. But I can live with this odd behavior. The main problem I have is that when I try to add another entry in the EmployeePasswordHistory list and then save the Employee entity,
I get this exception:
javax.persistence.EntityNotFoundException: Unable to find org.model.EmployeePasswordHistory with id Employee id : 10034
It's as if it tries to fetch my new EmployeePasswordHistory entry from the database.
The main thing I want to accomplish is that I want to be able to have my composite primary key values (four in total) populated from the values in my Employee Entity and be able to add new entries
in the EmployeePasswordHistory table.
FYI, I tried generating these entities from the database tables using Eclipse, since I was running out of options. It did not help.
I hope I was clear enough,
Thanks in advance for any help you can provide,
Christian