I have an entity called CNV which contains another entity called Individual. Individuals have patientIds which are unique. When I save a CNV, it also saves an Individual for me, using the CASCADE.SAVE_UPDATE annotation. However, it creates a new Individual each time and disregards the unique constraint of the patientId. I feel like I'm "missing something" in my flow, so would appreciate any suggestions - thanks!
Code:
@Entity
@Table(name="INDIVIDUAL")
public class Individual {
...
@Column(name="PATIENT_ID", unique=true)
private String patientId;
...
Code:
@Entity
@Table(name="CNV")
public class CNV implements Serializable {
...
@OneToOne
@Cascade(CascadeType.SAVE_UPDATE)
@JoinColumn(name="PATIENT_ID")
private Individual individual;
...
Code:
private void createAndSaveCNV(String labID, int chromosome, int start, int end, int size, int copyNumChange, int gains, int losses) {
session.beginTransaction();
Individual ind = new Individual();
ind.setPatientId(labID);
GenomicLocation gl = new GenomicLocation();
gl.setChromosome(chromosome);
gl.setStart(start);
gl.setEnd(end);
gl.setSize(size);
CNV cnv = new CNV();
cnv.setIndividual(ind);
cnv.setLocation(gl);
cnv.setCopyNumberChange(copyNumChange);
cnv.setCnvGains(gains);
cnv.setCnvLosses(losses);
session.save(cnv);
session.getTransaction().commit();
}