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