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