title: How to insert and update 3 table by Creating a Composite-ID class for the composite key using JPA annotation
body: <p>There are three tables HouseEntity, HousePersonEntity and PersonEntity.</p>
  
  <p>HouseEntity is identified with unique combination of houseNumber and houseName.
  This entity has no primary key it has houseNumber and houseName as composit key.</p>
  
  <p>PersonEntity has id (autogenerated) and name.</p>
  
  <p>HousePersonEntity is logical table which stores that one house with multiple person id. so in short HouseEntity has one to many relationship with PersonEntity.</p>
  
  <p>JPA entities </p>
  
  <pre><code>@Entity
  @Table(name="House")
  public class HouseEntity extends AbstractObject implements Serializable {
       @EmbeddedId
       private HouseEntityPK pk;          //Composit key
       //bi-directional many-to-one association to Dependent 
       @OneToMany(mappedBy="primaryKey.houseEntity ", cascade=CascadeType.ALL) 
       private Set&lt;HousePersonEntity &gt; housePersonEntity = new HashSet&lt;HousePersonEntity&gt;(); 
  
  @Embeddable
  public class HouseEntityPK implements Serializable {     
      @Column(name = "H_HOUSENUMBER", unique = true, nullable = false)
      private int houseNumber;
      @Column(name = "H_HOUSENAME", unique = true, nullable = false)
      private String houseName;
  
  @Entity
  @Table(name="Person")
  public class PersonEntity{     
     @Id
     @GeneratedValue(strategy=GenerationType.SEQUENCE, generator = "PERSON_SEQ")
     @Column(name = "ID", unique = true, nullable = false)
     private int id;
     private String name;
  
  @Entity
  @Table(name="HouseHavingPersons")
  @AssociationOverrides({
      @AssociationOverride(name = "primaryKey.personEntity",joinColumns = @JoinColumn(name = "ID")),
      @AssociationOverride(name = "primaryKey.houseEntity",  joinColumns = { 
                  @JoinColumn(name = "H_HOUSENUMBER" , referencedColumnName = "H_HOUSENUMBER") ,
                  @JoinColumn(name = "H_HOUSENAME" , referencedColumnName = "H_HOUSENAME") })
          })
  public class HousePersonEntity extends AbstractObject implements Serializable {
      @EmbeddedId
      private HousePersonEntityPK pKey= new HousePersonEntityPK();
      @Transient
      public PersonEntity getPersonEntity() {
          return getPrimaryKey().getPersonEntity();
      }
      @Transient
      public HouseEntity getHouseEntity() {
          return getPrimaryKey().getHouseEntity();
      }
  
  @Embeddable
  public class HousePersonEntityPK implements Serializable {     
      @ManyToOne(cascade=CascadeType.ALL)
      private HouseEntity houseEntity ;
      @ManyToOne(cascade=CascadeType.ALL)
      private PersonEntity personEntity ;
  </code></pre>
  
  <p>In <code>HouseHavingPersons</code>, when I insert data it first inserts in <code>House</code> and then inserts all the people names associted with that house in the <code>HouseHavingPersons</code> table having all the details of house and persons in it. This works fine. For all new associations it inserts in first in House and then populates in <code>HouseHavingPersons</code>.</p>
  
  <p>The problem starts when I try to update any record like if houseNumber is already present in table <code>House</code> and only few colums of been updated and new person is added or updated then it does not work. It only updates the <code>House</code> table and no entry is made in <code>HouseHavingPersons</code>. What am I doing wrong? Is there any other approch for this scenario? Any help will be appreciated. </p>
  
  <p>Edited .. for more explanation</p>
  
  <p>Hi Nicholas ,I got one link which have similar kind of example this is for your referance .
  <a href="http://www.codejava.net/frameworks/hibernate/hibernate-many-to-many-association-with-extra-columns-in-join-table-example" rel="nofollow noreferrer">http://www.codejava.net/frameworks/hibernate/hibernate-many-to-many-association-with-extra-columns-in-join-table-example</a> -- see Creating a Composite-ID class for the composite key.    </p>
  
  <p>Here my problem in Update not insert. on update i am able to update the House table but no update is happening on HousePersonEntity and goes on infinite loop. </p>
  
  <p>scenarios to be tested are 
  1. If no data for House id is present then insert in House and then insert associted Peoples id along with people id , hosue id, name.</p>
  
  <ol start="2">
  <li><p>if entry for house id is already present then insert the updated people entry in HousePersonEntity.</p></li>
  <li><p>if entry is already present in House table but few colums of House need to be updated then update House table for that house id and then insert in HousePersonEntity. in HousePersonEntity  if entry is present then update the existing field and for new data insert record in HousePersonEntity.</p></li>
  </ol>
  
  <p>So one house can have multiple people and that association should exist in HousePersonEntity table.</p>