-->
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.  [ 1 post ] 
Author Message
 Post subject: Persist object w/o loading its <many-to-one>
PostPosted: Sat Jan 06, 2007 6:48 pm 
Newbie

Joined: Sat Jan 21, 2006 8:30 pm
Posts: 6
I'm pretty much sure it has been answered before...but couldn't find it.

A "Blog Item" can have many "Blog Comment"s.
BlogItem has a Set<BlogComment>
And BlogComment as a reference to the BlogItem it belongs to.

I'm trying to save the BlogComment directly.
So instead of doing something like:
1. Load BlogItem with its comments
2. Add the comment to that list (which would make the comment persistent automatically)
...I'm trying something like:
1. Set the BlogItem "id" in BlogComment
2. Save BlogComment
Example:
Code:
public void addComment(String pText, int pBlogItemID) {
BlogItem oItem = new BlogItem();
oItem.setId(pBLogItemID);
BlogComment oComment = new BlogComment();
oComment.setText(pText);
oComment.setBlogItem(oItem);
...
oSession.save(oComment);
...
}


When executing this addComment() method, I get the following exception:
Code:
org.hibernate.PropertyValueException: not-null property references a null or transient value: ...BlogComment.blogItem


My thought was that I'm doing less, compared to first load the whole BlogItem data and then save the comment.

Q1. Is my way really wrong?
Q2. Is there any way I can make it work without having to load the BlogItem beforehand?
Q3. If doing it like in Hibernate tutorial/examples, what would happen if 2 threads try to add comments to the same BlogItem?
Example:
thread1: Load BlogItem (with comments)
thread2: Load BlogItem (with comments)
thread1: Add BlogComment to BlogItem's list
thread2: Add BlogComment to BlogItem's list (--> thread1's BlogComment is not part of thread2's BlogItem's list!!!)


Thanks,
Luc.


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

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.