本文介绍了MySQLIntegrityConstraintViolationException用于Hibernate hsql的删除。无法删除或更新父行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有父母过滤器实体和一个方向列出< Ad>广告作为 @OneToMany 关系。我尝试使用Hibernate hql 查询删除超过一周大的广告,但得到:
User:
@Entity @Table(name="users") public class User { ... @OneToMany(fetch = FetchType.LAZY, cascade=CascadeType.ALL, mappedBy="user") @OrderBy("id") private Set<Filter> filters = new HashSet<Filter>(0); ...
Filter:
@Entity @Table(name="filters") public class Filter { ... // I also tried use @OneToMany(CascadeType.REMOVE, orphanremoval=true) // or @OneToMany(cascade=CascadeType.ALL,orphanRemoval=true) @OneToMany private Set<Ad> ads; ...
Ad:
@Entity @Table(name="ads") public class Ad { ... private Date insertTime = new Date(); ...
DAO delete method:
public void deleteOldAds(Date date){ String hql = "delete from Ad where insertTime < :date"; session.createQuery(hql).setParameter("date", date).executeUpdate(); }
解决方案
The cause of the issue is that a join table is used if @OneToMany association side is the owner of the relationship.
To specify that a join column (foreign key) in the ads table is used for the association instead of a separate join table, simply map the association with a @JoinColumn:
@OneToMany @JoinColumn private Set<Ad> ads;
这篇关于MySQLIntegrityConstraintViolationException用于Hibernate hsql的删除。无法删除或更新父行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!