开发者

Hibernate One to many

开发者 https://www.devze.com 2023-01-16 10:36 出处:网络
As per hibernate documentation: 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 inser

As per hibernate documentation:

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  
    publi开发者_JAVA百科c Set<Soldier> getSoldiers() {  
    ...  
}  

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

My questions are:

  1. What is the advantage of such a setup. Why not create Manytonone side as the owning side
  2. Why in this setup these two values are needed: insertable=false, updatable=false
0

精彩评论

暂无评论...
验证码 换一张
取 消