-->
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.  [ 4 posts ] 
Author Message
 Post subject: problem with merging
PostPosted: Wed Dec 12, 2007 10:18 am 
Newbie

Joined: Fri Nov 09, 2007 7:29 pm
Posts: 3
Hi folks,

I got a many-to-many association of two Collections - exhibits:tags.
The user is able to add or remove tags of an exhibit. Now the problem when the exhibit x has already a tag y and the user wants to add some new tags, the "old" tag y is inserted when I call merge. So I got duplicate entries in my database but have to avoid that. Why is Hibernate trying to insert an entry which already exists? I did override the equals and hashCode methods on both sides of the association (exhibits:tags) with no success.

How can I tell Hibernate that it should only do an insert when the tag is new (not already saved in DB)?


Top
 Profile  
 
 Post subject: Re: problem with merging
PostPosted: Wed Dec 12, 2007 10:52 am 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
2chilled wrote:
Hi folks,

I got a many-to-many association of two Collections - exhibits:tags.
The user is able to add or remove tags of an exhibit. Now the problem when the exhibit x has already a tag y and the user wants to add some new tags, the "old" tag y is inserted when I call merge. So I got duplicate entries in my database but have to avoid that. Why is Hibernate trying to insert an entry which already exists? I did override the equals and hashCode methods on both sides of the association (exhibits:tags) with no success.

How can I tell Hibernate that it should only do an insert when the tag is new (not already saved in DB)?



Send us your mapping files, classes and the code that leads to the merging point. It doesn't look like it needs to insert the object.


Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 12, 2007 11:06 am 
Newbie

Joined: Fri Nov 09, 2007 7:29 pm
Posts: 3
The mapping of exhibit side:

Code:
@ManyToMany(fetch = FetchType.EAGER, cascade = {javax.persistence.CascadeType.MERGE, javax.persistence.CascadeType.PERSIST})
@Fetch(FetchMode.JOIN)
@JoinTable(name = "exhibit_tags", joinColumns = { @JoinColumn(name = "exhibit_id") }, inverseJoinColumns = { @JoinColumn(name = "tag_id") })
   @Basic(optional = true)
   public Set<Tag> getTags()
   {
      return this.tags;
   }


Mapping of tag side:
Code:
@ManyToMany(fetch = FetchType.EAGER, targetEntity = Exhibit.class, mappedBy = "tags")
@Sort(type = SortType.COMPARATOR, comparator = Exhibit.class)
@Fetch(FetchMode.JOIN)
@Basic(optional = true)
   public SortedSet<Exhibit> getExhibits()
   {
      return this.exhibits;
   }

In my ExhibitDAO I update an exhibit like this:
Code:
public Exhibit update(Exhibit exhibit)
   {
      if (exhibit != null)
      {
         getSession().merge(exhibit);
      }
      return exhibit;
   }

The n:m relation table is mapped like this:
Code:
@Entity

@Table(name = "exhibit_tags")

public class ExhibitTagRelation implements Serializable{

   private Integer id;
   private Exhibit exhibit;
   private Tag tag;
   public ExhibitTagRelation() {

   }
   @Id
   @GeneratedValue
   public Integer getId() {

      return id;
   }

   public void setId(Integer id) {

      this.id = id;
   }

   @ManyToOne(optional = false)

   @Fetch(FetchMode.JOIN)

   @JoinColumn(name = "exhibit_id")

   public Exhibit getExhibit() {

      return this.exhibit;

   }
   public void setExhibit(Exhibit exhibit) {

      this.exhibit = exhibit;

   }
   @ManyToOne(optional = false)

   @Fetch(FetchMode.JOIN)

   @JoinColumn(name = "tag_id")

   public Tag getTag() {

      return this.tag;

   }

   public void setTag(Tag tag) {

      this.tag = tag;

   }
   public String toString() {

      return this.getId().toString();

   }
}


I work with Spring WebMVC. Exhibit is my command object which I load like:
Code:
final Exhibit exhibit = exhibitDAO.find(ServletRequestUtils
              .getRequiredIntParameter(request, "id"));
//find method from exhibitDAO class:
public Exhibit find(Integer id)
   {
      return (Exhibit) getSession().createCriteria(Exhibit.class).add(Restrictions.eq("id", id)).uniqueResult();
   }


Please tell me if you need more information, and thanks for any answers!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 12, 2007 12:01 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
Why do you need to define ExhibitTagRelation entity? You don't really need to do this one. How do you add a tag-exhibit relation then? What is the id specification of tag and exhibit classes?



Farzad-


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.