0
votes

I'm having hard time understanding the following piece of documentation what does the comment

We need to duplicate the physical

means ?

What's the point of using insertable=false, updatable=false ?
Please, Can you help ?

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 some additional UPDATE statements.

@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() {

    ...

}
1
Is troop_fk referenced in another @Column on the Soldier class?Dean Clark

1 Answers

1
votes

We need to duplicate the physical

It's mean that Hibernate will link to column troop_fk in Soldier entity to connect with Troop entity. And we must write physical name of column that connecting Troop and Soldier.

What's the point of using insertable=false, updatable=false ?

Because it's foreign key to Troop entity and you can't edit it, because of constraint. First you create Troop and after that you add Soldier in this Troop.

This example about OneToMany map when owning side is OneToMany side, so this side own relationship and we assume that this side created first. You can delete insertable=false, updatable=false but if you have constraint in your DB and try to create Soldier with new Troop object you can get constraint violation error, because of this new Troop entity does not exists in Troop table.