简体   繁体   English

使用联接列单向一对多

[英]Unidirectional one-to-many using a Join Column

I've got the followings mappings (@OneToMany unidirectional). 我有以下映射(@OneToMany单向)。 The fk is placed in IssueManagement and when I insert a new issuemanagement, two sql statements are executed (insert into IssueManagement and then update the fk) rather than just insert! fk放在IssueManagement中,当我插入一个新的issuemanagement时,将执行两个sql语句(插入IssueManagement中,然后更新fk),而不是仅仅插入! I know that the fk is placed in owned table(IssueManagement) and the owner(Issue) is on the other side. 我知道fk放在拥有的表(IssueManagement)中,所有者(Issue)在另一侧。 Is there any workaround so that only one insert statement is executeD? 是否有任何解决方法,以便只执行一个insert语句?

@Entity
@Table
public class Issue {
    @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long id;

    @OneToMany(fetch=FetchType.EAGER, cascade={CascadeType.ALL})
    @JoinColumn(name="issue_id", nullable=false)
    List<IssueManagement> issueManagements= new ArrayList<IssueManagement>();

    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public List<IssueManagement> getIssueManagements() {
        return issueManagements;
    }
    public void addIssueManagment(IssueManagment issueManagment) {
        issueManagements.add(issueManagment);
    }
}

and

@Entity
@Table
public class IssueManagement {
    @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long id;

    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
}

The solution is to add these arguments to the @JoinColumn 解决方案是将这些参数添加到@JoinColumn

@JoinColumn(name = "parent_id", referencedColumnName = "id", nullable = false, insertable=false, updatable=false)

I still don't know why but this works! 我仍然不知道为什么,但这行得通! Much appreciated if anyone could explain why it works.. 非常感谢任何人都可以解释其工作原理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM