-->
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.  [ 5 posts ] 
Author Message
 Post subject: not able to update many-to-many relationship
PostPosted: Fri Oct 23, 2009 6:21 am 
Beginner
Beginner

Joined: Mon May 26, 2008 3:34 am
Posts: 31
Hello everyone,

I have two classes Article and Tag. An article can have a list of tags and a tag can be present in any article. I will show the relevant part of the code.
Article.java
Code:
public class Article implements Serializable, Auditable {
        ......
   private List<Tag> tagList = null;
        ......
}

Tag.java
Code:
public class Article implements Serializable, Auditable {
        ......
   private int id = 0;
   private String keyword = null;
   private int count = 0;
   private Timestamp creationDate = new Timestamp(new Date().getTime());
   private List<Article> articleList = null;
        ......
}


And here is the mapping files:
Article.hbm.xml
Code:
....
<bag name="tagList" table="article_tag" cascade="all">
      <key column="id_article" not-null="true"/>
      <many-to-many column="id_tag" class="com.model.metadata.Tag"/>
    </bag>
....


Tag.hbm.xml
Code:
....
<bag name="articleList" table="article_tag" cascade="all" inverse="true">
      <key column="id_tag" not-null="true"/>
      <many-to-many column="id_article" class="com.model.article.Article"/>
    </bag>
....


And this is the part of my code where i get the article data related to tags and insert/update my article
Code:
.....
List<Article> articleList = new ArrayList<Article>();   
         tagValues = StringUtils.split(value, " ");
         for (int i = 0; i < tagValues.length; i++) {
            this.tag = new Tag();
            this.tag.setKeyword(tagValues[i]);
            this.tag.setArticleList(articleList);
            this.tagList.add(this.tag);
         }
......
this.article.setTagList(this.tagList);
......


When i insert a new article with new tags everything works fine, the problem is when i try to update the article, my code does not update the existing tags, it inserts them again.

Any idea of what could be wrong?

Thanks a lot in advance.


Top
 Profile  
 
 Post subject: Re: not able to update many-to-many relationship
PostPosted: Mon Oct 26, 2009 6:06 am 
Beginner
Beginner

Joined: Mon May 26, 2008 3:34 am
Posts: 31
Any help on this? i am still stuck on this and not able to find a solution.

Thanks a lot in advance


Top
 Profile  
 
 Post subject: Re: not able to update many-to-many relationship
PostPosted: Mon Oct 26, 2009 10:25 am 
Beginner
Beginner

Joined: Mon Sep 14, 2009 9:29 am
Posts: 30
I think the object which u r updating is detached ...The solution to this prob can be using session.load(primarykey column)...


Top
 Profile  
 
 Post subject: Re: not able to update many-to-many relationship
PostPosted: Tue Oct 27, 2009 2:35 pm 
Beginner
Beginner

Joined: Mon May 26, 2008 3:34 am
Posts: 31
Thanks for the response pretham, but that did not solve my problem.

I have tried to modify the part of my code where i get the article data related to tags and insert/update my article
Code:
tagValues = StringUtils.split(value, " ");
         for (int i = 0; i < tagValues.length; i++) {
            this.tag = new Tag();
            this.tag.setKeyword(StringUtils.trim(tagValues[i], " "));            
            if(this.article.getTagList() != null) {
               this.article.getTagList().add(this.tag);
            } else {
               this.article.setTagList(new ArrayList<Tag>());
               this.article.getTagList().add(this.tag);
            }
            if(this.tag.getArticleList() != null) {
               this.tag.getArticleList().add(this.article);
            } else {
               this.tag.setArticleList(new ArrayList<Article>());
               this.tag.getArticleList().add(this.article);
            }
         }


But it did not work either, any other idea?

Thanks again


Top
 Profile  
 
 Post subject: Re: not able to update many-to-many relationship
PostPosted: Mon Nov 02, 2009 3:38 pm 
Beginner
Beginner

Joined: Mon May 26, 2008 3:34 am
Posts: 31
Hi again,

I was not be able to make it work.

Do i have to do this everytime i need to update my collection?
Code:
// if it does no exist in actual list i add it
         tagValues = StringUtils.split(value, " ");
         Tag object = null;
         boolean exist = false;
         for (int i = 0; i < tagValues.length; i++) {
            this.tag = new Tag();
            this.tag.setKeyword(StringUtils.trim(tagValues[i], " "));
            if(this.article.getTagList() != null) {
               for (Iterator iterator = article.getTagList().iterator(); iterator.hasNext();) {
                  object = (Tag) iterator.next();                  
                  if(object.getKeyword().equals(this.tag.getKeyword()))
                     exist = true;               
               }
            } else {
               this.article.setTagList(new ArrayList<Tag>());
            }
            if(!exist)
               this.article.getTagList().add(tag);
            exist = false;
         }
         // if it does not exist in the new list i remove it
         exist = false;         
         if(this.article.getTagList() != null) {
            for (Iterator iter = article.getTagList().iterator(); iter.hasNext();) {
               object = (Tag) iter.next();
               System.out.println(object.getId() + " - " + object.getKeyword());
               for (int i = 0; i < tagValues.length; i++) {               
                  this.tag = new Tag();
                  this.tag.setKeyword(StringUtils.trim(tagValues[i], " "));
                  
                  if(this.tag.getKeyword().equals(object.getKeyword()))
                     exist = true;                  
               }
               if(!exist) {                  
                  iter.remove();
                  object.setArticleList(null);
                  TagDAO tagDao = new TagDAO();
                  tagDao.makeTransient(object);
               }                  
               exist = false;
            }
         } else {
            this.article.setTagList(new ArrayList<Tag>());
         }


Please, is there any other solution to my issue?

Thanks again


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