These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: N-N error duplicate entry if use order
PostPosted: Wed Aug 31, 2005 6:14 am 
Newbie

Joined: Fri Mar 18, 2005 5:35 am
Posts: 8
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 :)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 31, 2005 8:47 am 
Newbie

Joined: Fri Mar 18, 2005 5:35 am
Posts: 8
Ok, solved it...
o forgot that hibernate perform an update on timestamp of object if i modify an relation associed...
in my hashCode i append the value of timestamp from CSBaseVersionModel...
public int hashCode() {
return new HashCodeBuilder().
append(uniqueId).
append(version).
toHashCode();
}
Sorry for unnecessary post
:)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.