0

When I am saving a List of objects by calling saveListOfPageChooserElement, it throws the below exception

Whereas, when I am saving a single instance by calling saveOrUpdate, then it works fine.

But to improve performance I want to save a List batch rather than single object at a time.

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 

3 Answers 3

1

You set ht Ids of all objects in the list:

abc.setId(0l);

And that's what causes the error. You cannot change an auto-generated ID by your own.

Remove this line.

Sign up to request clarification or add additional context in comments.

2 Comments

but i have to clear the id because the abc has a id then no new record will be inserted into database.and the current record will be updated.
If you use the same ID, it will update the current object and wont add another one.
0

In hibernate You can't set Id (Autogenrated) manulally like below.

abc.setId(0l);

Remove this above line try again.

1 Comment

if i remove this line then the previous record will be updated . because the abc has a id then no new record will be inserted into database.
0

The problem appears to be this line:

abc.setId(0l);

You are clearing the ids of the entities you've loaded from the database.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.