Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java OneToMany:hibernate尝试使外部引用无效,而不是删除整个子对象_Java_Hibernate - Fatal编程技术网

Java OneToMany:hibernate尝试使外部引用无效,而不是删除整个子对象

Java OneToMany:hibernate尝试使外部引用无效,而不是删除整个子对象,java,hibernate,Java,Hibernate,我有一个包含元素列表的实体“BigList”: @OneToMany( fetch = FetchType.LAZY, orphanRemoval=true ) @Cascade( { CascadeType.ALL } ) @JoinColumn( name = "ID_LIST", referencedColumnName="ID", updatable=true, insertable=true ) @OrderBy( value="position asc") L

我有一个包含元素列表的实体“BigList”:

  @OneToMany( fetch = FetchType.LAZY, orphanRemoval=true )
  @Cascade( { CascadeType.ALL } )  
  @JoinColumn( name = "ID_LIST", referencedColumnName="ID", updatable=true, insertable=true )
  @OrderBy( value="position asc")
  List<BigListElem> elements;
现在,如果我删除此列表中的一个元素并调用merge()方法(从而进行更新),则会出现以下异常:

impossible update ("DBNAME"."BIG_LIST_ELEM"."ID_LIST") to NULL
换句话说,它没有删除子级,而是尝试将外部引用设置为null,这会导致异常,因为“nullable”设置为false。 这是正确的,因为外部引用不能为null…问题是我不是要求将此引用设置为null,而是删除整个元素


这是为什么?

通常子元素上的FK约束可以为空,因为hibernate(如果我没记错的话)以这种方式进行删除:

  • 将子实体的FK设置为空
  • 删除子实体

  • 因此,如果在数据库级别强制执行NOTNULL约束,则必须手动从数据库中删除实体。

    对我来说似乎很奇怪……你确定至少没有一个简单的解决方法不涉及明确删除子实体吗?我不确定这一点。您可以尝试禁用NOTNULL约束并将show_sql切换为true。运行相同的测试,看看hibernate的行为如何是的,似乎hibernate将FK设置为null…现在我想知道..有没有办法截获此事件并跳过它以便转到实际的删除?我不这么认为。您可以改为删除该实例。
    impossible update ("DBNAME"."BIG_LIST_ELEM"."ID_LIST") to NULL