-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: cascade save not working
PostPosted: Fri Oct 20, 2006 10:59 pm 
Newbie

Joined: Mon Dec 27, 2004 11:37 pm
Posts: 4
cascade save does not work. when saving new objects, hibernates does an insert parent object but does update the child object. This results in
org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1

Hibernate should also call an sql insert child object. below is the relevant information

Hibernate version:
3.2.0 GA
Mapping documents:
<class name="Account" table="account">

<id name="id" type="int" unsaved-value="null">
<column name="account_id" sql-type="int" not-null="true" />
<generator class="increment" />
</id>

<property name="creationDate" column="CREATION_DATE" />
<property name="name" />
<set name="users" inverse="true" cascade="all">
<key column="account_id" />
<one-to-many class="User"/>
</set>
</class>

<class name="User" table="users">

<id name="id" type="int" unsaved-value="null">
<column name="user_id" sql-type="int" not-null="true" />
<generator class="increment" />
</id>

<property name="userName" />
<many-to-one name="account" column="account_id" class="Account"/>
</class>
Code between sessionFactory.openSession() and session.close():
Session session = HibernateUtil.openSession();
Transaction tx = session.beginTransaction();
try {
Serializable id = session.save(obj);
tx.commit();
return id;
} catch (HibernateException ex) {
tx.rollback();
logger.error(ex);
ex.printStackTrace();
} finally {
session.close();
}
Full stack trace of any exception that occurs:
Hibernate: select max(account_id) from account
Hibernate: insert into account (CREATION_DATE, name, phone, ADDRESS_LINE1, ADDRESS_LINE2, ADDRESS_CITY, ADDRESS_PROVINCE, ADDRESS_ZIP, account_id) values (?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: update users set userName=?, first_name=?, last_name=?, password=?, mobile=?, account_id=? where user_id=?
2006-10-21 10:52:21,655 ERROR [main] (BatchingBatcher.java:51) - Exception executing batch:
org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
at org.hibernate.jdbc.Expectations$BasicExpectation.checkBatched(Expectations.java:61)
at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:46)
at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:68)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:242)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:140)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at cn.com.blackdragonmedia.util.PersistenceManager.save(PersistenceManager.java:19)
at cn.com.blackdragonmedia.SaveAccount.main(SaveAccount.java:16)
2006-10-21 10:52:21,661 ERROR [main] (AbstractFlushingEventListener.java:301) - Could not synchronize database state with session
org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
at org.hibernate.jdbc.Expectations$BasicExpectation.checkBatched(Expectations.java:61)
at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:46)
at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:68)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:242)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:140)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at cn.com.blackdragonmedia.util.PersistenceManager.save(PersistenceManager.java:19)
at cn.com.blackdragonmedia.SaveAccount.main(SaveAccount.java:16)
2006-10-21 10:52:21,666 ERROR [main] (PersistenceManager.java:23) - org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
Hibernate: select max(user_id) from users
org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
at org.hibernate.jdbc.Expectations$BasicExpectation.checkBatched(Expectations.java:61)
at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:46)
at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:68)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:242)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:140)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at cn.com.blackdragonmedia.util.PersistenceManager.save(PersistenceManager.java:19)
at cn.com.blackdragonmedia.SaveAccount.main(SaveAccount.java:16)
Hibernate: insert into users (userName, first_name, last_name, password, mobile, account_id, user_id) values (?, ?, ?, ?, ?, ?, ?)
2006-10-21 10:52:21,687 WARN [main] (JDBCExceptionReporter.java:71) - SQL Error: 0, SQLState: null
2006-10-21 10:52:21,687 ERROR [main] (JDBCExceptionReporter.java:72) - Batch entry 0 insert into users (userName, first_name, last_name, password, mobile, account_id, user_id) values (Sonnny@gmail.com, Sonny, To, foobar, 13761375417, 3, 2) was aborted. Call getNextException to see the cause.
2006-10-21 10:52:21,688 WARN [main] (JDBCExceptionReporter.java:71) - SQL Error: 0, SQLState: 23503
2006-10-21 10:52:21,688 ERROR [main] (JDBCExceptionReporter.java:72) - ERROR: insert or update on table "users" violates foreign key constraint "users_account_id_fkey"
Detail: Key (account_id)=(3) is not present in table "account".
2006-10-21 10:52:21,689 ERROR [main] (AbstractFlushingEventListener.java:301) - Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:249)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:139)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at cn.com.blackdragonmedia.util.PersistenceManager.save(PersistenceManager.java:19)
at cn.com.blackdragonmedia.SaveAccount.main(SaveAccount.java:17)
Caused by: java.sql.BatchUpdateException: Batch entry 0 insert into users (userName, first_name, last_name, password, mobile, account_id, user_id) values (Sonnny@gmail.com, Sonny, To, foobar, 13761375417, 3, 2) was aborted. Call getNextException to see the cause.
at org.postgresql.jdbc2.AbstractJdbc2Statement$BatchResultHandler.handleError(AbstractJdbc2Statement.java:2512)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1310)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:347)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeBatch(AbstractJdbc2Statement.java:2574)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:242)
... 9 more
2006-10-21 10:52:21,728 ERROR [main] (PersistenceManager.java:23) - org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:249)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:139)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at cn.com.blackdragonmedia.util.PersistenceManager.save(PersistenceManager.java:19)
at cn.com.blackdragonmedia.SaveAccount.main(SaveAccount.java:17)
Caused by: java.sql.BatchUpdateException: Batch entry 0 insert into users (userName, first_name, last_name, password, mobile, account_id, user_id) values (Sonnny@gmail.com, Sonny, To, foobar, 13761375417, 3, 2) was aborted. Call getNextException to see the cause.
at org.postgresql.jdbc2.AbstractJdbc2Statement$BatchResultHandler.handleError(AbstractJdbc2Statement.java:2512)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1310)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:347)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeBatch(AbstractJdbc2Statement.java:2574)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:242)
... 9 more
Name and version of the database you are using:
postgres 8.1
The generated SQL (show_sql=true):
Hibernate: select max(account_id) from account
Hibernate: insert into account (CREATION_DATE, name, phone, ADDRESS_LINE1, ADDRESS_LINE2, ADDRESS_CITY, ADDRESS_PROVINCE, ADDRESS_ZIP, account_id) values (?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: update users set userName=?, first_name=?, last_name=?, password=?, mobile=?, account_id=? where user_id=?

Debug level Hibernate log excerpt:


Problems with Session and transaction handling?

Read this: http://hibernate.org/42.html


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 21, 2006 2:40 am 
Senior
Senior

Joined: Tue Aug 23, 2005 8:52 am
Posts: 181
That worked well for me except that it threw a "Detached entity passed to persist" when trying with your exact mapping file. The reason was that you had defined the primary keys to be integers and had mentioned the unsaved-value to be null. Im not sure if JDK 1.5 will make this workable(due to autoboxing) but dint work in my jdk 1.4 environment.

When i modified your mapping-file to have unsaved-value to be 0, and had a test code something like
Code:
        Account acc = new Account();
        acc.setName("Acc1");
        acc.setCreationDate("today");
        User user = new User();
        user.setUserName("me");
        user.setAccount(acc);
        acc.getUsers().add(user);
        s.persist(acc);


it inserted a parent record in the Account table and referenced it in the User Table.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 21, 2006 5:58 am 
Newbie

Joined: Mon Dec 27, 2004 11:37 pm
Posts: 4
you were right. It was an issue with auto boxing. I didn't change the mapping file to get it to work. I just changed my primary key from int to Integer and it now works like a charm. This should filed as a bug.


Top
 Profile  
 
 Post subject: Batch update returned unexpected row count
PostPosted: Wed Nov 29, 2006 6:14 pm 
Newbie

Joined: Thu Feb 24, 2005 8:49 am
Posts: 6
we are also having same issue but with some difference and here is the complete stack trace and interresting thing is this happens only in the LINUX box and works good in Windows enivironment.

Any idea?

Code:
2006-11-29 16:37:56,257 DEBUG [org.donorschoose.model.services.ProposalManagerImpl] - <Updating the proposal>
2006-11-29 16:37:56,257 DEBUG [org.donorschoose.model.dao.ProposalDataAccessImpl] - <The current proposal status is
id: 330239 workflow state id: 1 workflow state : draft>
2006-11-29 16:37:56,257 DEBUG [org.springframework.orm.hibernate3.HibernateTemplate] - <Found thread-bound Session for HibernateTemplate>
2006-11-29 16:37:56,257 DEBUG [org.springframework.orm.hibernate3.HibernateTemplate] - <Not closing pre-bound Hibernate Session after HibernateTemplate>
2006-11-29 16:37:56,257 DEBUG [org.springframework.orm.hibernate3.HibernateTransactionManager] - <Triggering beforeCommit synchronization>
2006-11-29 16:37:56,257 DEBUG [org.springframework.orm.hibernate3.HibernateTransactionManager] - <Triggering beforeCompletion synchronization>
2006-11-29 16:37:56,257 DEBUG [org.springframework.orm.hibernate3.HibernateTransactionManager] - <Initiating transaction commit>
2006-11-29 16:37:56,257 DEBUG [org.springframework.orm.hibernate3.HibernateTransactionManager] - <Committing Hibernate transaction on Session [org.hibernate.impl.SessionImpl@1b4b91f]>
2006-11-29 16:37:56,257 DEBUG [org.hibernate.impl.SessionImpl] - <automatically flushing session>
Hibernate: select nextval ('resource_id_seq')
Hibernate: select resourcety_.resourcetypeid, resourcety_.description as descript2_74_ from resourcetype resourcety_ where resourcety_.resourcetypeid=?
Hibernate: select nextval ('resourceattribute_id_seq')
Hibernate: insert into resource (details, price, procurementsystemid, vendorId, resourcetypeid, proposalid, seq, resourceid) values (?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into resourceAttribute (type, name, value, required, resourceId, resourceattributeid) values (?, ?, ?, ?, ?, ?)
Hibernate: update resource set proposalid=?, seq=? where resourceid=?
2006-11-29 16:37:56,371 ERROR [org.hibernate.jdbc.AbstractBatcher] - <Exception executing batch: >
org.hibernate.HibernateException: Batch update returned unexpected row count from update: 0 actual row count: 2 expected: 1
        at org.hibernate.jdbc.BatchingBatcher.checkRowCount(BatchingBatcher.java:100)
        at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:79)
        at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:58)
        at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:195)
        at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235)
        at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:142)
        at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:297)
        at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
        at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:985)
        at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:333)
        at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
        at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:584)
        at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:496)
        at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:469)
        at org.springframework.transaction.interceptor.TransactionAspectSupport.doCommitTransactionAfterReturning(TransactionAspectSupport.java:266)
        at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:170)
        at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:176)
        at $Proxy6.saveDraft(Unknown Source)
        at org.donorschoose.web.control.TeacherNonMaterialsResourcesController.handleSubmit(TeacherNonMaterialsResourcesController.java:67)
        at org.donorschoose.web.control.TeacherResourcesController.onSubmit(TeacherResourcesController.java:86)
        at org.springframework.web.servlet.mvc.SimpleFormController.processFormSubmission(SimpleFormController.java:258)
        at org.springframework.web.servlet.mvc.AbstractFormController.handleRequestInternal(AbstractFormController.java:259)
        at org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:139)
        at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:44)
        at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:717)
        at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:658)
        at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:392)
        at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:357)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
        at org.donorschoose.web.filter.ActionFilter.doFilter(ActionFilter.java:101)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
        at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:174)
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
        at org.apache.jk.server.JkCoyoteHandler.invoke(JkCoyoteHandler.java:199)
        at org.apache.jk.common.HandlerRequest.invoke(HandlerRequest.java:282)
        at org.apache.jk.common.ChannelSocket.invoke(ChannelSocket.java:754)
        at org.apache.jk.common.ChannelSocket.processConnection(ChannelSocket.java:684)
        at org.apache.jk.common.ChannelSocket$SocketConnection.runIt(ChannelSocket.java:876)
        at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
        at java.lang.Thread.run(Thread.java:595)
2006-11-29 16:37:56,372 ERROR [org.hibernate.event.def.AbstractFlushingEventListener] - <Could not synchronize database state with session>
org.hibernate.HibernateException: Batch update returned unexpected row count from update: 0 actual row count: 2 expected: 1


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.