Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Hibernate惰性初始化帮助_Java_Database_Hibernate_Spring_Spring Aop - Fatal编程技术网

Java Hibernate惰性初始化帮助

Java Hibernate惰性初始化帮助,java,database,hibernate,spring,spring-aop,Java,Database,Hibernate,Spring,Spring Aop,我在尝试使用hibernate删除时遇到问题。当我尝试删除时,我得到一个异常,表示存在子项,并且存在FK冲突。我也想删除子项,但删除似乎不是层叠的。在尝试解决这个问题大约一周后,我读到我应该使用HibernateInterceptor保持会话打开,以便可以加载孩子。当我现在尝试执行此操作时,会出现以下错误: Failed to load portlet com.blah.blah.CommunicationsPortlet: java.lang.ClassCastException: $Prox

我在尝试使用hibernate删除时遇到问题。当我尝试删除时,我得到一个异常,表示存在子项,并且存在FK冲突。我也想删除子项,但删除似乎不是层叠的。在尝试解决这个问题大约一周后,我读到我应该使用HibernateInterceptor保持会话打开,以便可以加载孩子。当我现在尝试执行此操作时,会出现以下错误:

Failed to load portlet com.blah.blah.CommunicationsPortlet: java.lang.ClassCastException: $Proxy27 incompatible with com.blah.blah.HibernateCommunicationsDAOImpl
以下是我的映射文件的摘录:

<set name="communicationCountries" inverse="true" cascade="all,delete-orphan">
<key column="COM_ID" not-null="true" on-delete="cascade" />
<one-to-many class="com.blah.blah.CommunicationCountry"/>
</set>

我真的不知道我做错了什么。我没有一个非常复杂的模式,只有一个表可以生成儿童(国家)。你知道我能做些什么来解决这个问题吗?

如果你只想删除父项和子项,你不需要加载子项集合。您甚至不需要
get()
父级,使用
load()
就足够了:

public void deleteCommunication(Integer id) throws DataAccessException {
  HibernateTemplate hibernate = getHibernateTemplate();
  Communication existing = (Communication) hibernate.load(Communication.class, id);
  hibernate.delete(existing);
}
当然,这需要适当的映射/级联。您显示的设置映射摘录是可以的,但
设置除外。这相当棘手,原因有几个:

  • 数据库必须支持它,模式必须定义它。如果不是这样的话,你会得到一个错误
  • 如果将cascade设置为“ALL”,则此设置不会提高性能,因为Hibernate仍会加载每个子实体以检查进一步的关联。您需要将cascade设置为“save update”以使其正常工作,这将对托管生命周期的父子关系产生副作用
  • 因此,我建议暂时删除delete=“cascade”上的
    ,并且在您完全理解所有含义之前不要使用它

    您提到的HibernateInterceptor与所有这些无关(最肯定的是,与您编写的delete()方法无关);这是与UI层通信方法的一部分

    以下是如何对上述所有问题进行故障排除:

  • 暂时摆脱HibernateInterceptor
  • 编写一个单元测试,在一个事务中创建父/子事务,提交它,在第二个事务中删除它们,提交它
  • 如果(2)不起作用,则表示映射和/或模式有问题。把它们都贴在这里,我来看看
  • 如果(2)不起作用,而上面的delete()方法不起作用,那么在调用delete()的代码中的某个地方就会出现问题。也许您正在加载相同的
    通信
    实例并覆盖其
    通信国家
    集合

  • 在尝试删除实体之前,是否从dao加载该实体?i、 e.myformform=formdao.getForm();然后你做你的form.delete()。对不起,我浏览了hibernate。输入你的代码。我不知道为什么,但这对我来说很有效。非常奇怪,这是我以前使用的,只是我使用了.get()而不是.load()。非常感谢您的帮助,经过一周的思考,我终于可以放松了。Get()vs load()不应该在错误方面有什么问题。删除时删除
    =级联
    将:-)
    
    public void deleteCommunication(Integer id) throws DataAccessException
    {
    HibernateTemplate hibernate = getHibernateTemplate();
    Communication existing = (Communication)hibernate.get(Communication.class, id);
    hibernate.initialize( existing.getCommunicationCountries());
    hibernate.delete(existing);
    } 
    
    public void deleteCommunication(Integer id) throws DataAccessException {
      HibernateTemplate hibernate = getHibernateTemplate();
      Communication existing = (Communication) hibernate.load(Communication.class, id);
      hibernate.delete(existing);
    }