保存对象列表会引发Hibernate异常

时间:2013-02-27 11:09:09

标签: java hibernate

当我通过调用List保存saveListOfPageChooserElement个对象时,会抛出以下异常

然而,当我通过调用saveOrUpdate保存单个实例时,它可以正常工作。

但是为了提高性能,我想一次保存List批次而不是单个对象。

有人可以建议一次保存整个列表有什么问题吗?

 List<Abc> listabc = widgetCopyDAO
                    .fetchabcByPageId(id);


    for (Abc abc: listabc ) {
                abc.setLastUpdatedBy(null);
                abc.setLastUpdatedOn(null);
                abc.setCreatedBy(widgetCopyDTO.getUserName());
                abc.setCreatedOn(new Date());
                abc.setPageChooser(new PageChooser(chooser.getId()));



                abc.setId(0l);
                issuePageWidgetDAO.saveOrUpdate(abc);
            }
//  widgetCopyDAO.saveListOfPageChooserElement(listabc);


public void saveOrUpdate(Abc abc) {
        if (abc.getId() == 0) {
            Long id = (Long) this.getHibernateTemplate().save(
                    abc);
            abc.setId(id);
        } else {
            this.getHibernateTemplate().update(abc);
        }
    }


public void saveListOfPageChooserElement(
            List<Abc> listabc) {
        this.getHibernateTemplate().saveOrUpdateAll(listabc);

    }

例外是

org.springframework.orm.hibernate3.HibernateSystemException: identifier of an instance of com.mct.model.Abc was altered from 138 to 0; nested exception is org.hibernate.HibernateException: identifier of an instance of com.mct.model.Abc was altered from 138 to 0
    at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:676)
    at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
    at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:424)
    at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
    at org.springframework.orm.hibernate3.HibernateTemplate.findByCriteria(HibernateTemplate.java:1055)
    at org.springframework.orm.hibernate3.HibernateTemplate.findByCriteria(HibernateTemplate.java:1048)
    at com.mct.dao.WidgetCopyDAO.fetchPageChooserWithImagesByChooser(WidgetCopyDAO.java:82)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
    at org.springframework.aop.framework.adapter.ThrowsAdviceInterceptor.invoke(ThrowsAdviceInterceptor.java:126)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
    at org.springframework.aop.framework.adapter.AfterReturningAdviceInterceptor.invoke(AfterReturningAdviceInterceptor.java:50)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
    at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:50)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
    at $Proxy58.fetchPageChooserWithImagesByChooser(Unknown Source)
    at com.mct.service.widgethelper.ChooserWidget.copyWidget(ChooserWidget.java:676)
    at com.mct.service.widgethelper.ChooserWidget.copyAllWidgets(ChooserWidget.java:634)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown 

3 个答案:

答案 0 :(得分:1)

您可以在列表中设置所有对象的ht ID:

abc.setId(0l);

这就是导致错误的原因。 您无法自行更改自动生成的ID。

删除此行。

答案 1 :(得分:0)

在休眠状态下您无法手动设置Id(自动生成),如下所示。

abc.setId(0l);

删除上面的这一行再试一次。

答案 2 :(得分:0)

问题似乎是这一行:

abc.setId(0l);

您正在清除您从数据库加载的实体的ID。