简体   繁体   English

休眠删除,侵犯儿童

[英]Hibernate Delete, child violation

I am yet again stuck with trying to delete data with Hibernate.. 我再一次尝试使用Hibernate删除数据。

I am at point where I am starting to just stack annotation, hoping something would work... so far nothing has. 我到了开始堆叠注释的地步,希望有什么用……到目前为止,没有任何东西。

I need to delete old calculation when starting new for same time period. 在相同时间段开始新计算时,我需要删除旧计算。

I have the following query 我有以下查询

@Modifying
@QueryHints(value = @QueryHint(name = HINT_FETCH_SIZE, value = "10"))
@Query(value = "DELETE FROM Group a WHERE a.c_date BETWEEN :dateA AND :dateB")
void deleteOld(@Param("dateA") LocalDate dateA, @Param("dateB") LocalDate dateB);

which uses entity Group, which has (on top of normal String, LocalDate and long types) attribute 它使用实体组,该组具有(在常规String,LocalDate和long类型之上)属性

    @OneToMany(cascade=CascadeType.ALL, mappedBy = "owner", orphanRemoval = true)
    @JsonManagedReference
    @OnDelete(action = OnDeleteAction.CASCADE)
    private List<Instrument> instruments = new ArrayList<>();

But I still get violated - child record found every time I try to run delete method. 但我仍然受到violated - child record found每次尝试运行delete方法时都会violated - child record found

I keep finding more and more annotations like this, from threads where people have the same kind of problems, but I would love to understand why is this a problem. 在人们遇到相同问题的线程中,我不断找到越来越多的此类注释,但是我很想了解为什么这是一个问题。 From what I read Cascade and orphanRemoval should be all I need, but it sure does not seem to be. 从我读到的内容来看,Cascade和orphanRemoval应该是我所需要的,但肯定不是。

Hibernate: 5.2.17.Final 休眠:5.2.17。最终

Please help me to understand, why is this happening ? 请帮助我了解为什么会发生这种情况?

The @OnDelete will delete records using a ON DELETE rule on the database, when using Hibernate to generate the schema. 使用Hibernate生成架构时, @OnDelete将使用数据库上的ON DELETE规则删除记录。 If you manage your own schema this will have no effect. 如果您管理自己的架构,则将无效。

The @QueryHints you have specified doesn't really make sense here, for an DELETE query that is. 您指定的@QueryHints在这里实际上没有任何意义,对于DELETE查询而言。 Nothing will be fetched. 什么都不会得到。

The fact that you are using an @Query basically bypasses the configuration in the @OneToMany , simply due to the fact that you write a query and apparently know what you are doing. 使用@Query的事实基本上会绕过@Query的配置,这@OneToMany是由于您编写了一个查询并且显然知道您在做什么。 So the mapping isn't taken into account. 因此不考虑映射。

If you want to delete the childs as then you have 3 options: 如果要删除子项,则有3个选项:

  1. Add an additional query and first remove the childs, then the parents 添加其他查询,然后先删除子项,然后删除父项
  2. Add an ON DELETE rule to your database, to automatically remove the childs ON DELETE规则添加到数据库中,以自动删除子项
  3. Retrieve the Group and remove using EntityManager.remove which will take the @OneToMany mappings into account as now Hibernate needs to manage the dependencies between the entities. 检索Group并使用EntityManager.remove删除,这将考虑@OneToMany映射,因为Hibernate现在需要管理实体之间的依赖关系。

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

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