简体   繁体   English

保存的对象列表将引发休眠异常

[英]Saving List of Objects Throws Hibernate Exception

When I am saving a List of objects by calling saveListOfPageChooserElement , it throws the below exception 当我通过调用saveListOfPageChooserElement保存对象List时,它将引发以下异常

Whereas, when I am saving a single instance by calling saveOrUpdate , then it works fine. 而当我通过调用saveOrUpdate保存单个实例时,则可以正常工作。

But to improve performance I want to save a List batch rather than single object at a time. 但是为了提高性能,我想一次保存一个List批而不是单个对象。

Can anyone suggest what's the problem with saving a whole list at once? 谁能建议一次保存整个列表有什么问题?

 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);

    }

The exception is 例外是

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 

You set ht Ids of all objects in the list: 您在列表中设置所有对象的ht Ids:

abc.setId(0l);

And that's what causes the error. 这就是导致错误的原因。 You cannot change an auto-generated ID by your own. 您不能自己更改自动生成的ID。

Remove this line. 删除此行。

In hibernate You can't set Id (Autogenrated) manulally like below. 在休眠状态下,您无法手动设置Id(自动生成),如下所示。

abc.setId(0l);

Remove this above line try again. 删除此行上方,然后重试。

The problem appears to be this line: 问题似乎是此行:

abc.setId(0l);

You are clearing the ids of the entities you've loaded from the database. 您正在清除从数据库中加载的实体的ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM