-->
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: Issues with @OneToMany Bidirectional relationships
PostPosted: Sun Jul 15, 2007 2:56 pm 
Newbie

Joined: Sun Jul 15, 2007 1:56 pm
Posts: 2
I'm using Hibernate 3.3.2.ga, Hibernate EntityManager 3.3.1.ga, with Hibernate Annotations 3.3.0.ga

I have a configuration identical to the @OneToMany Bidirectional one in the documentation:


Code:
@Entity
public class Troop {
    @OneToMany(mappedBy="troop")
    public Set<Soldier> getSoldiers() {
    ...
}

@Entity
public class Soldier {
    @ManyToOne
    @JoinColumn(name="troop_fk")
    public Troop getTroop() {
    ...
}


However, whenever I do something like this:

Code:
Troop troop = new Troop();
HashSet soldierSet = new HashSet();

soldierSet.add(new Soldier("soldier1"));
soldierSet.add(new Soldier("soldier2"));

troop.setSoldiers(soldierSet);

mgr.save(troop);


All three objects are saved, but the troop_fk column in each Soldier is null.

Seeing that, as expected, adding @JoinColumn(name="troop_fk", nullable="false") throws a PersistenceException

It seems logical that one would want the troop_fk to be automatically populated -- is there a configuration I'm missing here? Or is it just expected that I will have to:

1) create the Troop first (no soldiers)
2) do a setTroop(troop) on each new Soldier
3) add the Soldiers to the Troop
4) re-save the Troop?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 15, 2007 4:35 pm 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
The way to fix this is to setTroop on each of your soldiers. You don't have to save the troop first. If this were a unidirectional relationship then hibernate would populate the keys for you.

The behaviour you're seeing is probably by design. You've defined the relationship as bi-directional but haven't initialised the objects correctly to represent this relationship.

Mike


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 15, 2007 5:52 pm 
Newbie

Joined: Sun Jul 15, 2007 1:56 pm
Posts: 2
Thanks Mike,

It makes sense that I have to set the Troop in each Soldier (and don't have to save it first), but it would be very nice if Hibernate detected the situation I initially posted about and kind of "helped" you by setting the "One" side in the "Many"s. Little things like that go a long way (to me at least!)

Best Regards,
Jeff


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 15, 2007 8:01 pm 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
I appreciate your point that it might be convenient not to have to initialise the reverse relationship but it would mean that what you read back from the DB was not the same as you wrote. In the majority of situations you wouldn't want this. You might argue for a special case to deal with this but I think a consistent, predictable approach is better.

Mike


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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.