I created a JPA unidirectional join table following the JPA documentation very closely (http://www.jpox.org/docs/1_2/jpa_orm/one_to_many_list.html).
I could insert an applicant, A, with joblist (JobOne, JobTwo) without any problems. However, when I tried to insert a different applicant B, with a job already used by another applicant, e.g. joblist (JobOne), it threw an exception:
'Duplicate entry 'JobOne' for key 'jobId'
The documentation says The join table will NOT be given a primary key (so that duplicates can be stored)
but it does not look like they use composite key (applicantId, jobId) for the join table.
public class Applicant
{
private long applicantId;
private List<Job> jobList;
@OneToMany(fetch=FetchType.EAGER, cascade=CascadeType.ALL)
@JoinTable(name="applicant_job",
joinColumns={@JoinColumn(name="applicantId")},
inverseJoinColumns={@JoinColumn(name="jobId")})
public List<Job> getJobList() {
return jobList;
}
//getters, setters
}
public class Job
{
private long jobId;
//getters, setters
}