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.