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.  [ 3 posts ] 
Author Message
 Post subject: Updating a Model with Relationships
PostPosted: Fri May 16, 2008 1:39 am 
Newbie

Joined: Wed Aug 30, 2006 11:30 am
Posts: 3
I feel like I am having a kind of brain cramp. I have an entity that has a parent/child relationship to another. In this case its Episode and Comments. I am using @OneToMany and @ManyToOne respectively. The @OneToMany has mappedBy="episode" set.

I have a web framework (struts2) that will create an Entity by calling the default constructor and call the appropriate setters based on posted parameters.

In the past I swear that if I did not load the object from the db, then the Comments collection gets initialized to an empty collection in the constructor and that empty collection overwrites the existing Comments.

For some reason I am not seeing this behavior and after a lot of testing, googling, etc it looks like if I create an object, set the ID to an existing persistant object and then call getHibernateTemplate().merge() -- I am using Spring HibernateDaoSupport -- hibernate sees the empty collection and ignores it leaving the existing comments in tact.

So really this is actually kind of good behavior, unless you wanted to wipe out a collection by setting it an empty instance. I guess its just not what I expected and I am wondering if Hibernate is really smart enough to ignore an empty collection object when the persisted object has items?

I suppose that it would be pretty dangerous if Hibernate allowed you to wipe out a collection by accidently trying to persist an updated object without its collection(s) intialized.....

Can someone verify my theory or educate me?


Top
 Profile  
 
 Post subject: Bi-Directional Owner on Set side
PostPosted: Fri May 16, 2008 10:39 am 
Newbie

Joined: Wed Aug 30, 2006 11:30 am
Posts: 3
So I have done some more testing and I think the issue that when I do this:

@OneToMany(mappedBy = "episode")
Set<Comment> comments;

@ManyToOne
Episode episode;

I am telling Hibernate that the Comments side owns this relationship. I could make this unidirectional and I get the behavior I was expecting:

@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "episode_id")
@org.hibernate.annotations.Cascade(
value = org.hibernate.annotations.CascadeType.DELETE_ORPHAN
)
Set<Comment> comments;

but my real question is: How can I make the Parent (Set) side of the relationship the owning side, when its bidirectional?


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 16, 2008 2:18 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Hi,
I copied this from http://www.hibernate.org/hib_docs/annotations/reference/en/html_single/:
Quote:
To map a bidirectional one to many, with the one-to-many side as the owning side, you have to remove the mappedBy element and set the many to one @JoinColumn as insertable and updatable to false. This solution is obviously not optimized and will produce some additional UPDATE statements.


look at paragraph "2.2.5.3.2.1. Bidirectional" for the complete example.

_________________
Sanne
http://in.relation.to/


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