简体   繁体   English

休眠删除不会触发

[英]Hibernate delete just won't fire

I've ask'd this before but still have no clue what is going on... 我之前曾问过这个,但仍然不知道发生了什么...

I've read about entity needing to be cleared of relationships before deleting, and about cascade.all in parent entity, however i still dont get how is hibernate not doing anything... 我已经读过有关在删除之前需要清除实体关系的实体,以及有关父实体中的cascade.all的信息,但是我仍然不知道休眠是怎么做的……

@Transactional
public void deleteAllinRange(LocalDate a, LocalDate b) {
List<Invoice> z = invoiceRepo.selector(a,b);
    for(Invoice x : z){
        x.setOwner(null);
        invoiceRepo.delete(x);
    }

going into for loop z list size is 314, and there is all the entities i expect there to be. 进入for循环z列表的大小为314,并且有所有我期望的实体。 I then tried to setOwner to null, since that is the only relationship to parent element. 然后,我尝试将setOwner设置为null,因为那是与父元素的唯一关系。

After nulling owner, all the attributes in x entity are type long, string or localDate so surely there cant be any relationships to parent element? 使所有者为空后,x实体中的所有属性均为long,string或localDate类型,因此可以确定与父元素没有任何关系吗?

Parent element is set of Invoices with List of invoice elements 父元素是带有发票元素列表的发票集

@OneToMany(cascade=CascadeType.ALL, mappedBy = "owner", orphanRemoval = true)
@JsonManagedReference
private List<Invoice> invoices = new ArrayList<>();

java fill just run invoiceRepo.delete(x); Java invoiceRepo.delete(x);只需运行invoiceRepo.delete(x); x 314, but will actually do nothing at all and SQL debugging shows there is not even attempt to remove anything, noting is queried here... x 314,但实际上根本不执行任何操作,SQL调试显示甚至没有尝试删除任何内容,请注意此处。

Why is this happening? 为什么会这样呢? Why is there not any queries, no errors, no nothing. 为什么没有任何查询,没有错误,什么都没有。

When you setOwner and don't save the x, then it has no effect. 当您设置setOwner并且不保存x时,则无效。 Also, if you have a constraint nullable=false , you cannot set it to null. 另外,如果您具有约束nullable=false ,则不能将其设置为null。

What do you want to do? 你想让我做什么? Do you want to delete Invoices and remain the owners? 您要删除发票并保留所有者吗? Then you should remove the orphanRemoval = true . 然后,您应该删除orphanRemoval = true

Or else, if you want to remove the orphans, leave it as is and just delete the Invoices without setting the owner null. 否则,如果要删除这些孤儿,则将其保留原样,仅删除发票而不将所有者设置为null。 It will remove orphaned owners automatically. 它将自动删除孤立的所有者。

If you want to manage them manually, don't set a foreign key and break the relation between them, just delete any record when you want and set fields yourself null or value. 如果要手动管理它们,请不要设置外键并破坏它们之间的关系,只需在需要时删除任何记录,然后将字段设置为null或value。

In addition, you can also use the method delete(Iterable iterable) of the repository to delete multiple at once. 此外,您还可以使用存储库的delete(Iterable iterable)方法立即删除多个。

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

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