-->
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: Parent/Child Update
PostPosted: Tue Oct 12, 2010 3:48 am 
Newbie

Joined: Tue Oct 12, 2010 3:22 am
Posts: 3
I have the following classes and a question regarding how to update the Article class when I add a new comment.

Code:
public class Article {
   ...
   
   @OneToMany(mappedBy = "article")
   private Set<Comment> comments;
}

public class Comment {
   ...
   
   @ManyToOne(cascade=CascadeType.ALL)
   @JoinColumn(name="ARTICLE_ID")
   private Article article;
}


Right now I'm doing this, but I want to know if I can somehow omit the save(comment) and let Hibernate do it.

Code:
Comment comment = new Comment(...);
article.addComment(comment);
getSession().save(comment);
getSession().merge(article);


I want to know if it is possible to have some mapping that allow me to do something like this:

Code:
Comment comment = new Comment(...);
article.addComment(comment);
getSession().merge(article);


Last edited by LorenzoR on Tue Oct 12, 2010 4:02 am, edited 2 times in total.

Top
 Profile  
 
 Post subject: Re: Parent/Child Update
PostPosted: Tue Oct 12, 2010 3:57 am 
Newbie

Joined: Tue Oct 12, 2010 3:22 am
Posts: 3
Solved! All I had to do was change this:

Code:
@OneToMany(mappedBy = "article")


For this:
Code:
@OneToMany(mappedBy = "article", cascade = CascadeType.ALL)


But now I get this from console when, for example, an article has 6 comments:

Code:
Hibernate: insert into COMMENT (ARTICLE_ID, DATE, TEXT) values (?, ?, ?)
Hibernate: update ARTICLE set TEXT=?, TITLE=? where ARTICLE_ID=?
Hibernate: update COMMENT set ARTICLE_ID=?, DATE=?, TEXT=? where COMMENT_ID=?
Hibernate: update COMMENT set ARTICLE_ID=?, DATE=?, TEXT=? where COMMENT_ID=?
Hibernate: update COMMENT set ARTICLE_ID=?, DATE=?, TEXT=? where COMMENT_ID=?
Hibernate: update COMMENT set ARTICLE_ID=?, DATE=?, TEXT=? where COMMENT_ID=?
Hibernate: update COMMENT set ARTICLE_ID=?, DATE=?, TEXT=? where COMMENT_ID=?


And more lines of Hibernate: update COMMENT... each time I add a new comment. Is that correct?


Top
 Profile  
 
 Post subject: Re: Parent/Child Update
PostPosted: Tue Oct 12, 2010 8:37 am 
Newbie

Joined: Tue Feb 02, 2010 10:37 am
Posts: 13
LorenzoR,
You have to put inverse="true" in comments field of Article class.


Top
 Profile  
 
 Post subject: Re: Parent/Child Update
PostPosted: Tue Oct 12, 2010 3:43 pm 
Newbie

Joined: Tue Oct 12, 2010 3:22 am
Posts: 3
Thanks for the answer vijaybangy, but I have read in the documentation that mappedby is equivalent to inverse=true.


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.