我有以下映射(@OneToMany单向)。 fk放在IssueManagement中,当我插入一个新的issuemanagement时,执行两个sql语句(插入到IssueManagement然后更新fk)而不是插入!我知道fk被置于自有表(IssueManagement)中,而所有者(Issue)位于另一侧。有没有解决方法,只有一个插入语句是executeD?
@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);
}
}
和
@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;
}
}
答案 0 :(得分:0)
解决方案是将这些参数添加到@JoinColumn
@JoinColumn(name = "parent_id", referencedColumnName = "id", nullable = false, insertable=false, updatable=false)
我仍然不知道为什么但这有效!非常感谢,如果有人能解释它为什么会起作用..