Hi, i read
http://www.hibernate.org/hib_docs/v3/re ... ons-sorted and
http://www.hibernate.org/109.html
i have an n-n relation from group and resource
In Workgroup.hbm.xml
<set name="resources" table="cs_workgroupResource" lazy="true"
inverse="true" cascade="none" sort="it.unitn.cs.core.model.util.ResourceComparator">
<cache usage="read-write" />
<key column="fk_workgroupId" />
<many-to-many class="it.unitn.cs.core.model.Resource" column="fk_resourceTreeUniqueId" />
</set>
In Resource.hbm.xml
<set name="workgroups" table="cs_workgroupResource" lazy="true" inverse="false" cascade="none" sort="it.unitn.cs.core.model.util.WorkgroupComparator">
<cache usage="read-write" />
<key column="fk_resourceTreeUniqueId" />
<many-to-many class="it.unitn.cs.core.model.Workgroup" column="fk_workgroupId" />
</set>
I implemented hashCode and equals for both class:
// CSBaseVersionModel is a class that conteins an Integer id and Timestamp for versioning
public class Resource extends CSBaseVersionModel {
private Integer resourceType;
private String name;
private Date expirationDate;
private Collection<Workgroup> workgroups = new TreeSet<Workgroup>();
private void setWorkgroups(Collection<Workgroup> workgroups) {
this.workgroups = workgroups;
}
public Collection<Workgroup> getWorkgroups() {
return this.workgroups;
}
public void addWorkgroup(Workgroup workgroup){
if(workgroup == null){
return;
}
this.getWorkgroups().add(workgroup);
workgroup.getResources().add(this);
}
public void removeWorkgroup(Workgroup workgroup){
if(workgroup == null){
return;
}
this.getWorkgroups().remove(workgroup);
workgroup.getResources().remove(this);
}
public Date getExpirationDate() {
return expirationDate;
}
public void setExpirationDate(Date expirationDate) {
this.expirationDate = expirationDate;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getResourceType() {
return resourceType;
}
public void setResourceType(Integer resourceType) {
this.resourceType = resourceType;
}
public String toString() {
return new ToStringBuilder(this).
appendSuper(super.toString()).
append("resourceType", resourceType).
append("name", name).
append("expirationDate", expirationDate).
toString();
}
public int hashCode() {
return new HashCodeBuilder().appendSuper(super.hashCode()).
append(name).
append(resourceType).
append(expirationDate).
toHashCode();
}
public boolean equals(Object o) {
if(this == o) return true;
if(null == o) return false;
if (!(o instanceof Resource)) {
return false;
}
Resource temp = (Resource) o;
return new EqualsBuilder().appendSuper(super.equals(o)).
append( this.resourceType, temp.resourceType).
append(this.name, temp.name).
append( this.expirationDate, temp.expirationDate).
isEquals();
}
}
public class Workgroup extends CSBaseVersionModel {
private String name;
private String description;
private boolean isDefault;
private Collection<Resource> resources = new TreeSet<Resource>();
private void setResources(Collection<Resource> resources) {
this.resources = resources;
}
public Collection<Resource> getResources() {
return this.resources;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public boolean getIsDefault() {
return isDefault;
}
public void setIsDefault(boolean isDefault) {
this.isDefault = isDefault;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString() {
return new ToStringBuilder(this).
appendSuper(super.toString()).
append("name", name).
append("description", description).
append("isDefault", isDefault).
toString();
}
public int hashCode() {
return new HashCodeBuilder().appendSuper(super.hashCode()).
append(name).
append(isDefault).
append(description).
toHashCode();
}
public boolean equals(Object o) {
if(this == o) return true;
if(null == o) return false;
if (!(o instanceof Workgroup)) {
return false;
}
Workgroup temp = (Workgroup) o;
return new EqualsBuilder().appendSuper(super.equals(o)).
append( this.name, temp.name).
append(this.description, temp.description).
append( this.isDefault, temp.isDefault).
isEquals();
}
}
Now if i delete an workgroup i manage the relation for Resource with this code:
Workgroup preavious = (Workgroup)session.load(Workgroup.class,workgroupId);
ArrayList<Resource> resources = new ArrayList<Resource>(preavious.getResources());
for(Resource resTemp: resources){
resTemp.removeWorkgroup(preavious);
session.update(resTemp);
}
if the resources conteins other workgroups when update it i obtain an error because hibernate try to re-insert the other workgroup in association table...
but if i remove the "order" attribute in <set ../> all works...
I don't understand the problem :)