-->
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: BiDirectional OneToMany Associations and Ownership
PostPosted: Wed Jul 26, 2006 3:48 pm 
Beginner
Beginner

Joined: Fri Jan 16, 2004 11:05 am
Posts: 33
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:
Hibernate 3.2cr2
Hibernate Annotations 3.2 cr1

We have some confusion here as to what is meant by owner side of a relationship in the hibernate_annotations.pdf reference on page 18.

Both examples are using the same Objects making it unclear, atleast to us.

From the reference manual page 18.
"Since many to one are (almost) always the owner side of a bidirectional relationship in the EJB3 spec, the one
to many association is annotated by @OneToMany( mappedBy=... )"

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

"Troop has a bidirectional one to many relationship with Soldier through the troop property. You don't have to
(must not) define any physical mapping in the mappedBy side."

Is this refering to the database ownership or the logical ownership?


From the reference manual page 19.
"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."

Code:
@Entity
public class Troop {
@OneToMany
@JoinColumn(name="troop_fk") //we need to duplicate the physical information
public Set<Soldier> getSoldiers() {
...
}
@Entity
public class Soldier {
@ManyToOne
@JoinColumn(name="troop_fk", insertable=false, updatable=false)
public Troop getTroop() {
...
}



Is the reference to "one-to-many" meaning the side annotated with @OneToMany?

We are trying to determine which way the mapping should be done if logically we say that a Troop owns a set of Sodiers. Which also means that to create a new Soldier you would have to do this by adding the Soldier to the Troop. So, the child is always added via the parent, it can never be added on its own.

However, from a database point of view, the Soldier is the owner because he is the one holding the foreign key.

Hopefully this question makes sense and I appreciate the clarification


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 26, 2006 5:20 pm 
Beginner
Beginner

Joined: Fri Jan 16, 2004 11:05 am
Posts: 33
To make this more clear I think the difference is in the logical relationship.

Given the following UML:

Image

Would be the first example in the manual and mapped as following:

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




Given the following UML:

Image


Would be the second example in the manual and mapped as following:

Code:
@Entity
public class Parent{
@OneToMany
@JoinColumn(name="parent_fk") //we need to duplicate the physical information
public Set<Offspring> getOffspring() {
...
}
@Entity
public class Offspring{
@ManyToOne
@JoinColumn(name="parent_fk", insertable=false, updatable=false)
public Parent getParent() {
...
}



Is this correct?

Hopefully this made the situation more clear.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 28, 2006 2:14 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Composition and aggregation have nothing to do with assoication ownership.
association ownership means when I update the owner side in my object model, the change will trigger a database update of the FK(s).

There is no logical concept in it

_________________
Emmanuel


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.