-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Saving entity with unique constraint
PostPosted: Thu Jun 28, 2012 1:05 pm 
Newbie

Joined: Thu Jun 28, 2012 12:52 pm
Posts: 3
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();
   }


Top
 Profile  
 
 Post subject: Re: Saving entity with unique constraint
PostPosted: Thu Jun 28, 2012 2:24 pm 
Newbie

Joined: Thu Jun 28, 2012 12:52 pm
Posts: 3
Nevermind! I fixed it by checking to see if the Individual was already present. My mistake - I thought Hibernate would do this for me. Revised code:

Code:
private void createAndSaveCNV(String labID, int chromosome, int start, int end, int size, int copyNumChange, int gains, int losses) {
      session.beginTransaction();

      Individual ind;      
      Object matches = session.createQuery("from Individual as ind where ind.patientId='" + labID + "'").uniqueResult();
      
      if (matches != null) {
         ind = (Individual) matches;
      } else {
         ind = new Individual();
         ind.setPatientId(labID);
      }
      ...
   }


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.