There are three tables HouseEntity, HousePersonEntity and PersonEntity.
HouseEntity is identified with unique combination of houseNumber and houseName. This entity has no primary key it has houseNumber and houseName as composit key.
PersonEntity has id (autogenerated) and name.
HousePersonEntity is logical table which stores that one house with multiple person id. so in short HouseEntity has one to many relationship with PersonEntity.
JPA entities
@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<HousePersonEntity > housePersonEntity = new HashSet<HousePersonEntity>();
@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 ;
In HouseHavingPersons
, when I insert data it first inserts in House
and then inserts all the people names associted with that house in the HouseHavingPersons
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 HouseHavingPersons
.
The problem starts when I try to update any record like if houseNumber is already present in table House
and only few colums of been updated and new person is added or updated then it does not work. It only updates the House
table and no entry is made in HouseHavingPersons
. What am I doing wrong? Is there any other approch for this scenario? Any help will be appreciated.
Edited .. for more explanation
Hi Nicholas ,I got one link which have similar kind of example this is for your referance . http://www.codejava.net/frameworks/hibernate/hibernate-many-to-many-association-with-extra-columns-in-join-table-example -- see Creating a Composite-ID class for the composite key.
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.
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.
if entry for house id is already present then insert the updated people entry in HousePersonEntity.
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.
So one house can have multiple people and that association should exist in HousePersonEntity table.