I have a many to may relationship CohortGroup and Employee. Any time I insert an Employee into the CohortGroup hibernate deletes the group from the resolution table and inserts all the members again, plus the new one. Why not just add the new one?
The annotation in the Group:
@ManyToMany(cascade = { PERSIST, MERGE, REFRESH })
@JoinTable(name="MYSITE_RES_COHORT_GROUP_STAFF",
joinColumns={@JoinColumn(name="COHORT_GROUPID")},
inverseJoinColumns={@JoinColumn(name="USERID")})
public List<Employee> getMembers(){
return members;
}
The other side in the Employee
@ManyToMany(mappedBy="members",cascade = { PERSIST, MERGE, REFRESH } )
public List<CohortGroup> getMemberGroups(){
return memberGroups;
}
Code snipit
Employee emp = edao.findByID(cohortId);
CohortGroup group = cgdao.findByID(Long.decode(groupId));
group.getMembers().add(emp);
cgdao.persist(group);
below is the sql reported in the log
delete from swas.MYSITE_RES_COHORT_GROUP_STAFF where COHORT_GROUPID=?
insert into swas.MYSITE_RES_COHORT_GROUP_STAFF (COHORT_GROUPID, USERID) values (?, ?)
insert into swas.MYSITE_RES_COHORT_GROUP_STAFF (COHORT_GROUPID, USERID) values (?, ?)
insert into swas.MYSITE_RES_COHORT_GROUP_STAFF (COHORT_GROUPID, USERID) values (?, ?)
insert into swas.MYSITE_RES_COHORT_GROUP_STAFF (COHORT_GROUPID, USERID) values (?, ?)
insert into swas.MYSITE_RES_COHORT_GROUP_STAFF (COHORT_GROUPID, USERID) values (?, ?)
insert into swas.MYSITE_RES_COHORT_GROUP_STAFF (COHORT_GROUPID, USERID) values (?, ?)
This seams really inefficient and is causing some issues. If sevral requests are made to add an employee to the group then some get over written.
Seams like equals and hashCode might be a reason for this. Below are the implementation for these methods. Any red flags?
CohortGroup
@Override
public int hashCode() {
final int prime = 31;
int result = getName().hashCode();
result = prime * result + ((emp == null) ? 0 : emp.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {return true;}
if (!(obj instanceof CohortGroup)) {return false;}
CohortGroup other = (CohortGroup) obj;
if(!getName().equals(other.getName())){return false;}
if (emp == null && other.getOwner() != null) {
return false;
} else if (!emp.equals(other.getOwner())) {
return false;
}
return true;
}
Employee
@Override
public boolean equals(Object obj) {
if (this == obj) {return true;}
if (obj == null) {return false;}
if (!(obj instanceof Employee)) {return false;}
Employee other = (Employee) obj;
if (EMPLID == null && other.getEMPLID() != null) {
return false;
} else if (!EMPLID.equals(other.getEMPLID())) {
return false;
}
return true;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((EMPLID == null) ? 0 : EMPLID.hashCode());
return result;
}
I have added an addMember method to the CohortGroup that adds to both sides of the relationship:
public void addMember(Employee emp){
this.getMembers().add(emp);
emp.getMemberGroups().add(this);
}
Continued thanks to all that are helping.