休眠删除不会级联 [英] Hibernate delete doesn't cascade
问题描述
我有一些链接如下的实体:
I have a few entities linked as follows:
@Entity
@Table(name = "distribution_activity")
public class DistributionActivity extends AbstractActivity {
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "activity", orphanRemoval = true)
protected Set<DistributionTask> tasks = new TreeSet<>();
...
}
和
@Entity
@Table(name = "distribution_task")
public class DistributionTask extends AbstractTask {
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "activity_id")
protected DistributionActivity activity;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "store_id")
protected Store store;
...
}
和
@Entity
@Table(name = "store")
public class Store extends AbstractAuditableEntity {
@OneToMany(cascade = CascadeType.REMOVE, fetch = FetchType.EAGER, mappedBy = "store", orphanRemoval = true)
protected Set<DistributionTask> distributionTasks = new TreeSet<>();
...
}
存储库如下:
@Repository
public interface DistributionActivityRepository extends PagingAndSortingRepository<DistributionActivity, Long> {
}
和
@Repository
public interface StoreRepository extends PagingAndSortingRepository<Store, Long> {
}
我正在使用MySQL,并且生成的表没有外键上的任何级联选项.当我删除DistributionActivity时,一切正常,并且Hibernate实际上为每个链接的任务发出delete语句.
I'm using MySQL and the tables are generated WITHOUT any cascade options on the foreign keys. When I delete a DistributionActivity everything works fine and Hibernate actually issues delete statements for each of the linked tasks.
hibernate.SQL:109 - delete from distribution_task where id=? and version=?
hibernate.SQL:109 - delete from distribution_activity where id=? and version=?
但是,当我删除商店时,不会为链接的任务生成任何delete语句,并且会抛出MySQLIntegrityConstraintViolationException异常,该异常指的是外键冲突.
When I delete a Store however, no delete statements are generated for the linked tasks and a MySQLIntegrityConstraintViolationException exception is thrown referring to a foreign key violation.
hibernate.SQL:109 - delete from store where id=? and version=?
有任何线索吗?
推荐答案
可以在删除发生的session.delete()周围发布代码段吗?
Can you post the snippet of code around session.delete() where the deletion happens?
我不能肯定地说出来,但是在涉及双向关系之前,我曾遇到过类似的问题.简而言之,在使用双向关系时,我需要确保对象是同步的.
I cannot say it for sure but I have ran in similar issues before related to bidirectional relationships. Briefly speaking, I needed to make sure the objects are in sync when using bidirectional relationships.
在我看来,您要删除在DistributionTask中仍然具有引用的商店.
It looks to me that you want to delete a Store that still have a reference in DistributionTask.
例如,如果您有类似这样的内容:
Example, if you have something like this:
session.delete(store);
然后,尝试更改以下内容:
Then, try changing for something like this:
distributionTask.setStore(null);
session.save(distributionTask);
session.delete(store);
所以;处理双向关系时,您需要手动控制对象的一致性.
So; you need control the consistency of your objects manually when dealing with bidirectional relationship.
我希望它会有所帮助.干杯,
I hope it helps. Cheers,
这篇关于休眠删除不会级联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!