休眠从onetomany中删除
我有两个OneToMany关系表
class ServiceProvider {
...
@OneToMany(fetch = FetchType.EAGER,mappedBy =“serviceProvider”,cascade = {CascadeType.ALL,CascadeType.REMOVE},orphanRemoval = true)@OnDelete(action = OnDeleteAction.CASCADE)private List serviceCenters; ...
}
class ServiceCenterDetails {
... //双向多对一关联到ServiceProviderDomainMap @ManyToOne @JoinColumn(name =“SERVICE_PROVIDER_ID”)private ServiceProvider serviceProvider;
...
}
我正在尝试删除提供的行。 但我得到错误:
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:由造成无法删除或更新父行,外键约束失败( fixoline
。 service_center_details
,约束FK_qvahoxeovx9vmwl6mcu2c0lyw
外键( SERVICE_PROVIDER_ID
)参考service_provider
( ID
))
下面是我尝试的方式
String hql = "DELETE FROM ServiceProvider WHERE id = :providerId";
Query query = sessionFactory.getCurrentSession().createQuery(hql);
query.setParameter("providerId",providerId);
int result = query.executeUpdate();
有人可以帮助解决它吗?
错误信息非常清楚:有些外键引用了您要删除的ServiceProvider
。 先删除ServiceCenterDetails
:
delete from ServiceCenterDetails where serviceProvider.id = :providerId
链接地址: http://www.djcxy.com/p/37043.html