本文介绍了违反完整性约束(...) - 发现子记录,Java,Oracle,Hibernate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在更新加密时,我不需要更新值,但在删除加密时删除所有的子值:

  class Crypto {
$ b @OneToMany(cascade = CascadeType.ALL,orphanRemoval = true)
@JoinColumn(name =cryptoId,updatable = false)
private Set< Values>值;
...
}


class值{
...
@ManyToOne
@JoinColumn(name = cryptoId)
私人密码加密;
...
}

删除加密时的SQL调试:

lock
Hibernate:从Crypto中删除其中id=?

错误:

DB是Oracle
为什么Hibernate在删除Crypto之前,不要删除他们所有的子项Values values?

解决方案

如果您通过hql删除Crypto对象,hibernate不能级联删除操作。由于级联是hibernate配置问题(即hbm.xml或注释),因此必须将数据返回给Java以处理级联。在HQL中执行UPDATE / DELETE行修改完全发生在数据库服务器上。



你可以做什么:


  1. 获取所有应该删除的Crypto实例。为他们每个人。

  2. 调用entityManager.remove()或session.delete()。


I need no update Values when update Crypto, but delete all child Values when delete Crypto:

class Crypto {
...
    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
    @JoinColumn(name="cryptoId", updatable=false)
    private Set<Values> values;
...
}


class Values {
...
    @ManyToOne
    @JoinColumn(name = "cryptoId")
    private Crypto crypto;
...
}

SQL debug when I delete Crypto:

Error:

DB is OracleWhy Hibernate, before delete "Crypto", don't delete all their child "Values" values?

解决方案

If you are deleting the Crypto object through hql, hibernate cannot not cascade the delete operation. Since the cascade is a hibernate configuration matter (i.e. hbm.xml or annotations) the data must be returned to Java for processing of cascades. Doing UPDATE/DELETE row modifications in HQL happens entirely on the database server.

What you can do:

  1. fetch all Crypto instances that should be removed. for each of them.
  2. Call entityManager.remove() or session.delete().

这篇关于违反完整性约束(...) - 发现子记录,Java,Oracle,Hibernate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 13:53