I am going through the Hibernate documentation for bidirectional relationship, in the doc it says that:
Example 7.21. Bidirectional one to many with many to one side as association owner
@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.
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 not optimized and will produce additional UPDATE statements.
Example 7.22. Bidirectional association with one to many side as owner
@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() {
...
}
I am finding difficulty in understanding this as I am new to Hibernate.
1) What it means when the doc says:
You don't have to (must not) define any physical mapping in the mappedBy side.
2) @JoinColumn
in 7.22 has same value (troop_fk) for name
attribute. Can we specify different values? What is the advantage & disadvantages of setting insertable=false, updatable=false
here?
Can someone please explain?