title: saving references to other entities
body: <p>In a spring mvc application using hibernate over a MySQL database, I have a <code>Document</code> entity with each <code>document</code> object containing different types of <code>codes</code> that are all instances of a <code>Code</code> entity.  The <code>Code</code> entity contains a distinct list of possible values that are defined by a combination of <code>code.pk.code</code> and <code>code.pk.codesystem</code>, with <code>pk</code> being a reference to a composite primary key.  And each <code>code</code> object might be referenced by many <code>document</code> objects.</p>
  
  <p><strong>How do I map this in hibernate and MySQL?</strong></p>
  
  <p>The way I have things laid out now, I keep getting errors about violating primary key constraints for <code>Code</code> every time I try to store a <code>Document</code> containing any <code>code</code> that is already stored in the <code>codes</code> table of the underlying MySQL database.  Specifically, when my dao runs the following line of code:  </p>
  
  <pre><code>this.entitymanager.persist(document)  
  </code></pre>
  
  <p>The following error is immediately thrown:  </p>
  
  <pre><code>Hibernate: insert into codes (displayname, code, codesystem) values (?, ?, ?)
  WARN  SqlExceptionHelper - SQL Error: 1062, SQLState: 23000
  ERROR SqlExceptionHelper - Duplicate entry 'somecode-somecodesystem' for key 'PRIMARY'  
  </code></pre>
  
  <p>The program crashes at that point, so I don't know if it would otherwise next save the <code>document</code> to the <code>documents</code> table in the database.  Why <strong>does it try to save to the <code>codes</code> table?  Is there some way I can set things up so that it just saves the references to the <code>code</code> to the <code>documents</code> table?</strong>  It would be a nuisance to remove the relationship and just store the keys as strings in the <code>documents</code> table without joining <code>codes</code> to <code>document</code>.</p>
  
  <p>Here is my java:  </p>
  
  <pre><code>@Entity
  @Table(name = "documents")
  public class Document {
  
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(name = "id")
  public Integer id;
  
  @ManyToOne(cascade = {CascadeType.ALL}, fetch=FetchType.EAGER)
  @JoinColumns({ @JoinColumn(name = "confidentiality_code", referencedColumnName = "code"),
      @JoinColumn(name = "confidentiality_codesystem", referencedColumnName = "codesystem"),
  })
  public Code conftype;
  
  //other stuff, then getters and setters  
  }
  
  @Entity
  @Table(name = "codes")
  public class Code implements Serializable{
  
  private static final long serialVersionUID = 1L;
  
  @EmbeddedId
  public EmbedCodePK codePk;
  
  @Column(name="displayname")
  private String displayname;
  
  public EmbedCodePK getCodePk(){return codePk;} 
  public void setCodePk(EmbedCodePK mypk){codePk = mypk;}
  
  public String getDisplayname(){return displayname;}
  public void setDisplayname(String dspnm){displayname=dspnm;}
  
  }
  
  @Embeddable
  public class EmbedCodePK implements Serializable {
  
  private static final long serialVersionUID = 652312726451130179L;
  
  @Column(name="code", nullable=false)
  public String code;
  
  @Column(name="codesystem", nullable=false)
  private String codesystem;
  
  public EmbedCodePK() {}
  public EmbedCodePK(String c, String cs) {
      this.code = c;
      this.codesystem = cs;
  }
  
  /** getters and setters **/
  public String getCode(){return code;}
  public void setCode(String c){code=c;}
  
  public void setCodesystem(String cs) {this.codesystem = cs;}
  public String getCodesystem() {return codesystem;}
  
  @Override
  public boolean equals(Object obj) { 
      if (this == obj) return true;
      if (obj == null) return false;
      if (getClass() != obj.getClass()) return false;
      final EmbedCodePK other = (EmbedCodePK) obj;
      if (code == null) {
          if (other.code != null) return false;
      } else if (!code.equals(other.code)) return false;
      if (codesystem == null) {
          if (other.codesystem != null) return false;
      } else if (!codesystem.equals(other.codesystem)) return false;
      return true;
  }
  
  @Override
  public int hashCode() { 
      int hash = 3;
      hash = 53 * hash + ((code == null) ? 0 : code.hashCode());
      hash = 53 * hash + ((codesystem == null) ? 0 : codesystem.hashCode());
      return hash;
  }
  }
  </code></pre>