Java 休眠一对多删除列表
我有两个实体与一家公司有关系:Java 休眠一对多删除列表,java,hibernate,jpa,Java,Hibernate,Jpa,我有两个实体与一家公司有关系: @Entity @Table(name="FATHER") @Audited(withModifiedFlag=true) @XmlType public class Father{ @Column(name="FATHER_ID") private Long id; @OneToMany(fetch = FetchType.EAGER, mappedBy = "father", orphanRemoval = true, cascad
@Entity
@Table(name="FATHER")
@Audited(withModifiedFlag=true)
@XmlType
public class Father{
@Column(name="FATHER_ID")
private Long id;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "father", orphanRemoval = true, cascade = {CascadeType.ALL})
List<Son> childrens;
//getter and setter for id, only getter for childrens.
}
@Table(name="CHILDREN")
@Entity
@Audited(withModifiedFlag=true)
public class Children{
@Column(name="CHILDREN_ID")
private Long id;
@ManyToOne
@JoinColumn(name = "FATHER_ID", nullable = false, insertable = false, updatable = false)
private Father father;
}
我得到了以下例外:
3:51:17 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [appServlet] in context with path [/sample.web] threw exception [Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement] with root cause
java.sql.SQLIntegrityConstraintViolationException: ORA-01400: cannot insert NULL into ("MYDB"."CHILDREN_AUD"."FATHER_ID")
为什么hibernate不能从父对象中理解父id?我应该在儿童中设置父亲身份字段吗
我知道这里没有什么特别的,但我在搜索了几个小时后找不到任何解决方案
希望有人能帮上忙不太清楚,但我认为这就是正在发生的事情。当您调用
father.getChildren().clear()
时,您只是在中断父级与其子级之间的关系,这意味着father\u ID
被设置为null,子级被更新。这应该是可行的,因为orphanRemoving=true
,但我认为Hibernate Envers(我假设您正在使用它进行审计)不知何故会将其作为MarkupSubRule
的更新,而不是(或之前)删除,并尝试将NULL插入CHILDREN\u AUD.FATHER\u ID
。要检查Envers是否真的导致了此问题,您可以暂时禁用它,然后重试。如果可以,您可以在审核表CHILDREN\u AUD
中更改FATHER\u ID
以接受空值。首先,什么是“NLNK\u A”。“CHILDREN\u AUD”表?第二,孩子已经有了“父亲ID”;在联接列中定义,它充当父id字段的外键。但是,它被标记为insertable/updateable=false,因此不能通过关系对其进行更改,因此您需要对该字段进行其他映射,以便为其设置值。Chris,该表是子项的审核表。至于映射——你的意思是我必须有类似父权的东西(如问题中提到的)?
3:51:17 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [appServlet] in context with path [/sample.web] threw exception [Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement] with root cause
java.sql.SQLIntegrityConstraintViolationException: ORA-01400: cannot insert NULL into ("MYDB"."CHILDREN_AUD"."FATHER_ID")