Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
QUICK OVERVIEW:
I created a new object, set the id and desc and called the saveOrUpdate. The record does NOT exist in the database or cache, but it is trying to do an update (based on the trace below)?? What am I missing??
Hibernate version:
3.0.4
Mapping documents:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping schema="amlusdev" default-lazy="false">
<class name="com.gs.fw.cmpl.de.data.Account" table="DS_ACCOUNT_TB" lazy="false" batch-size="1000">
<cache usage="read-write"/>
<id name="accountId" type="string" unsaved-value="null" >
<column name="acct_id_c" not-null="true"/>
</id>
<property name="accountDesc" type="string" column="ACCT_DESC_C" />
<!-- set name="productList" inverse="true" lazy="false" fetch="join" order-by="sub_acct_c ASC, prime_id_c ASC">
<cache usage="read-write"/>
<key column="sub_acct_c"/>
<one-to-many class="com.gs.fw.cmpl.de.data.Product" />
</set> -->
</class>
<query name="DE_ACCTS_WITH_TRADES">
<![CDATA[
from Account order by accountId
]]>
</query>
<sql-query name="DE_ACCTS_SELECTSP" callable="true">
<return alias="Account" class="com.gs.fw.cmpl.de.data.Account">
<return-property name="accountId" column="acct_id_c"/>
<return-property name="accountDesc" column="ACCT_DESC_C"/>
</return>
{ ? = call selectAllEmployments() }
</sql-query>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
try
{
session.saveOrUpdate(_dataObject);
}
catch(HibernateException ex)
{
throw new DASCriticalException("ERROR: During Hibernate InsertOrUpdate operation.", ex);
}
Full stack trace of any exception that occurs:
org.hibernate.HibernateException: Unexpected row count: 0 expected: 1
at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:32)
at org.hibernate.persister.entity.BasicEntityPersister.update(BasicEntityPersister.java:1972)
at org.hibernate.persister.entity.BasicEntityPersister.updateOrInsert(BasicEntityPersister.java:1899)
at org.hibernate.persister.entity.BasicEntityPersister.update(BasicEntityPersister.java:2139)
at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:75)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:239)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:223)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:137)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:274)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:726)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:320)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:86)
at com.gs.fw.cmpl.ds.das.db.objects.DASHibernateNamedStatementImpl.commit(DASHibernateNamedStatementImpl.java:298)
at com.gs.fw.cmpl.ds.das.db.DASDatabaseServiceImpl.commit(DASDatabaseServiceImpl.java:91)
at com.gs.fw.cmpl.ds.tests.DataAccessServiceTests.dasInsertUpdateDeleteTest(DataAccessServiceTests.java:359)
at com.gs.fw.cmpl.ds.tests.DataAccessServiceTests.hibernateTests(DataAccessServiceTests.java:576)
at com.gs.fw.cmpl.ds.tests.DataAccessServiceTests.main(DataAccessServiceTests.java:619)
Name and version of the database you are using:
DB2 UDB v8.x
The generated SQL (show_sql=true):
Debug level Hibernate log excerpt:
DEBUG [main] (SessionFactoryImpl.java:260) - instantiated session factory
INFO [main] (SessionFactoryImpl.java:377) - Checking 1 named queries
DEBUG [main] (SessionFactoryImpl.java:384) - Checking named query: DE_ACCTS_WITH_TRADES
DEBUG [main] (QueryTranslatorImpl.java:207) - parse() - HQL:
from com.gs.fw.cmpl.de.data.Account order by accountId
DEBUG [main] (QueryTranslatorImpl.java:223) - --- HQL AST ---
\-[QUERY] 'query'
+-[SELECT_FROM] 'SELECT_FROM'
| \-[FROM] 'from'
| \-[RANGE] 'RANGE'
| \-[DOT] '.'
| +-[DOT] '.'
| | +-[DOT] '.'
| | | +-[DOT] '.'
| | | | +-[DOT] '.'
| | | | | +-[DOT] '.'
| | | | | | +-[IDENT] 'com'
| | | | | | \-[IDENT] 'gs'
| | | | | \-[IDENT] 'fw'
| | | | \-[IDENT] 'cmpl'
| | | \-[IDENT] 'de'
| | \-[IDENT] 'data'
| \-[IDENT] 'Account'
\-[ORDER] 'order'
\-[IDENT] 'accountId'
DEBUG [main] (ErrorCounter.java:72) - throwQueryException() : no errors
DEBUG [main] (HqlSqlBaseWalker.java:120) - query() << begin, level = 1
DEBUG [main] (FromElement.java:81) - FromClause{level=1} : com.gs.fw.cmpl.de.data.Account (no alias) -> account0_
DEBUG [main] (FromReferenceNode.java:48) - Resolved : accountId -> account0_.acct_id_c
DEBUG [main] (HqlSqlBaseWalker.java:125) - query() : finishing up , level = 1
DEBUG [main] (HqlSqlWalker.java:331) - processQuery() : ( SELECT ( FromClause{level=1} amlusdev.DS_ACCOUNT_TB account0_ ) ( order account0_.acct_id_c ) )
DEBUG [main] (HqlSqlWalker.java:451) - Derived SELECT clause created.
DEBUG [main] (JoinProcessor.java:112) - Using FROM fragment [amlusdev.DS_ACCOUNT_TB account0_]
DEBUG [main] (HqlSqlBaseWalker.java:128) - query() >> end, level = 1
DEBUG [main] (QueryTranslatorImpl.java:193) - --- SQL AST ---
\-[SELECT] QueryNode: 'SELECT' querySpaces (amlusdev.DS_ACCOUNT_TB)
+-[SELECT_CLAUSE] SelectClause: '{derived select clause}'
| +-[SELECT_EXPR] SelectExpressionImpl: 'account0_.acct_id_c as acct1_' {FromElement{explicit,not a collection join,not a fetch join,fetch non-lazy properties,classAlias=null,role=null,tableName=amlusdev.DS_ACCOUNT_TB,tableAlias=account0_,colums={,className=com.gs.fw.cmpl.de.data.Account}}}
| \-[SQL_TOKEN] SqlFragment: 'account0_.ACCT_DESC_C as ACCT2_2_'
+-[FROM] FromClause: 'from' FromClause{level=1, fromElementCounter=1, fromElements=1, fromElementByClassAlias=[], fromElementByTableAlias=[account0_], fromElementsByPath=[], collectionJoinFromElementsByPath=[], impliedElements=[]}
| \-[FROM_FRAGMENT] FromElement: 'amlusdev.DS_ACCOUNT_TB account0_' FromElement{explicit,not a collection join,not a fetch join,fetch non-lazy properties,classAlias=null,role=null,tableName=amlusdev.DS_ACCOUNT_TB,tableAlias=account0_,colums={,className=com.gs.fw.cmpl.de.data.Account}}
\-[ORDER] OrderByClause: 'order'
\-[SQL_TOKEN] IdentNode: 'account0_.acct_id_c' {originalText=accountId}
DEBUG [main] (ErrorCounter.java:72) - throwQueryException() : no errors
DEBUG [main] (QueryTranslatorImpl.java:177) - HQL:
from com.gs.fw.cmpl.de.data.Account order by accountId
DEBUG [main] (QueryTranslatorImpl.java:178) - SQL: select account0_.acct_id_c as acct1_, account0_.ACCT_DESC_C as ACCT2_2_ from amlusdev.DS_ACCOUNT_TB account0_ order by account0_.acct_id_c
DEBUG [main] (ErrorCounter.java:72) - throwQueryException() : no errors
DEBUG [main] (DASHibernateUtil.java:93) - In method getSessionFromSessionFactory(), sessionFactoryName: de_trade_surveil
DEBUG [main] (DASHibernateUtil.java:77) - In method getSessionFactory()
DEBUG [main] (SessionImpl.java:248) - opened session at timestamp: 4590333838626816
DEBUG [main] (JDBCTransaction.java:46) - begin
DEBUG [main] (AbstractBatcher.java:422) - opening JDBC connection
DEBUG [main] (JDBCTransaction.java:50) - current autocommit status: false
DEBUG [main] (Cascades.java:575) - id unsaved-value strategy NULL
DEBUG [main] (AbstractSaveEventListener.java:413) - detached instance of: com.gs.fw.cmpl.de.data.Account
DEBUG [main] (DefaultSaveOrUpdateEventListener.java:200) - updating detached instance
DEBUG [main] (DefaultSaveOrUpdateEventListener.java:246) - updating [com.gs.fw.cmpl.de.data.Account#7]
DEBUG [main] (DefaultSaveOrUpdateEventListener.java:296) - updating [com.gs.fw.cmpl.de.data.Account#7]
DEBUG [main] (JDBCTransaction.java:83) - commit
DEBUG [main] (SessionImpl.java:319) - automatically flushing session
DEBUG [main] (AbstractFlushingEventListener.java:52) - flushing session
DEBUG [main] (AbstractFlushingEventListener.java:102) - processing flush-time cascades
DEBUG [main] (AbstractFlushingEventListener.java:150) - dirty checking collections
DEBUG [main] (AbstractFlushingEventListener.java:167) - Flushing entities and processing referenced collections
DEBUG [main] (DefaultFlushEntityEventListener.java:121) - Updating entity: [com.gs.fw.cmpl.de.data.Account#7]
DEBUG [main] (AbstractFlushingEventListener.java:203) - Processing unreferenced collections
DEBUG [main] (AbstractFlushingEventListener.java:217) - Scheduling collection removes/(re)creates/updates
DEBUG [main] (AbstractFlushingEventListener.java:79) - Flushed: 0 insertions, 1 updates, 0 deletions to 1 objects
DEBUG [main] (AbstractFlushingEventListener.java:85) - Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
DEBUG [main] (Printer.java:83) - listing entities:
DEBUG [main] (Printer.java:90) - com.gs.fw.cmpl.de.data.Account{accountId=7, accountDesc=HIBERNATE UPDATE}
DEBUG [main] (AbstractFlushingEventListener.java:267) - executing flush
DEBUG [main] (BasicEntityPersister.java:1930) - Updating entity: [com.gs.fw.cmpl.de.data.Account#7]
DEBUG [main] (AbstractBatcher.java:277) - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
DEBUG [main] (AbstractBatcher.java:311) - update amlusdev.DS_ACCOUNT_TB set ACCT_DESC_C=? where acct_id_c=?
DEBUG [main] (AbstractBatcher.java:365) - preparing statement
DEBUG [main] (BasicEntityPersister.java:1602) - Dehydrating entity: [com.gs.fw.cmpl.de.data.Account#7]
DEBUG [main] (NullableType.java:59) - binding 'HIBERNATE UPDATE' to parameter: 1
DEBUG [main] (NullableType.java:59) - binding '7' to parameter: 2
Exception in thread "main" ERROR [main] (AbstractFlushingEventListener.java:277) - Could not synchronize database state with session
org.hibernate.HibernateException: Unexpected row count: 0 expected: 1