0

当我List通过调用保存对象时saveListOfPageChooserElement,它会引发以下异常

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

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

谁能建议一次保存整个列表有什么问题?

 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 
4

3 回答 3

1

您设置列表中所有对象的 ht Id:

abc.setId(0l);

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

删除此行。

于 2013-02-27T11:15:10.340 回答
0

在休眠中,您不能像下面那样手动设置 Id (Autogenrated)。

abc.setId(0l);

删除上面的这条线再试一次。

于 2013-02-27T11:15:08.140 回答
0

问题似乎是这一行:

abc.setId(0l);

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

于 2013-02-27T11:15:50.497 回答