-->
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.  [ 1 post ] 
Author Message
 Post subject: many-to-one causes UPDATE instead of INSERT
PostPosted: Tue Apr 17, 2007 11:29 pm 
Newbie

Joined: Wed Jul 07, 2004 12:52 pm
Posts: 9
I have 2 mapped classes which work fine when being saved independantly. However, when I set one as a reference to another and save the object holding the reference, Hibernate tries to do an UPDATE on both instead of an INSERT.
I am using a sequence generator and I have unsaved-value="0".
I am sure that the id property is 0 because it is a private transient long with no accessor methods.

What is strange is that Hibernate must know that these are new objects because it goes ahead and gets sequence values for them and sets them on the objects. But since it knows, why is it trying to UPDATE?

Any help would REALLY be appreciated (I've been playing with this for many hours).

Thanks.



Hibernate version:
3.1.3

Mapping documents:

<hibernate-mapping package="com.sknt.picasso.trade" default-lazy="false" default-cascade="all">
<class name="Trade" table="TRD_TRADE">
<id name="databasePrimaryKey" access="field" column="TRADE_KEY" type="long" unsaved-value="0">
<generator class="sequence">
<param name="sequence">S_TRD_TRADE</param>
</generator>
</id>
<many-to-one name="header" column="TRADE_HEADER_KEY" not-null="true"/>
</class>
</hibernate-mapping>

<hibernate-mapping package="com.sknt.picasso.trade" default-lazy="false" default-cascade="all">
<class name="TradeHeader" abstract="true" table="TRD_TRADE_HEADER">
<id name="databasePrimaryKey" access="field" column="TRADE_HEADER_KEY" type="long" unsaved-value="0">
<generator class="sequence">
<param name="sequence">S_TRD_TRADE_HEADER</param>
</generator>
</id>
<discriminator column="CHILD_CLASS"/>
</class>
</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():

If I just do:

Trade t = new Trade();
session.save(t);

or

TradeHeader th= new OTCTradeHeader();
session.save(th);

it works fine.

The problem is if I do:

Trade t = new Trade();
TradeHeader th= new OTCTradeHeader();
t.setHeader(th);
session.save(t);

Full stack trace of any exception that occurs:

007-04-17 23:17:13,151 ERROR [org.hibernate.event.def.AbstractFlushingEventListener] - Could not synchronize database state with session
org.hibernate.StaleStateException: Unexpected row count: 0 expected: 1
at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:27)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2204)
at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2118)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2374)
at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:91)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:232)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:140)
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:500)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:473)
at com.sknt.picasso.trade.saving.TradeSavingTestSuite.tearDown(TradeSavingTestSuite.java:67)
at junit.framework.TestCase.runBare(TestCase.java:130)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at junit.extensions.TestDecorator.basicRun(TestDecorator.java:22)
at junit.extensions.TestSetup$1.protect(TestSetup.java:19)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.extensions.TestSetup.run(TestSetup.java:23)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:128)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

Name and version of the database you are using:
Oracle 10.2.0.1.0

The generated SQL (show_sql=true):

Hibernate: select S_TRD_TRADE.nextval from dual
Hibernate: select S_TRD_TRADE_HEADER.nextval from dual
Hibernate: update TRD_TRADE set TRADE_ID=?, VERSION_ID=?, VERSION_NUMBER=?, VERSION_CREATOR=?, VERSION_COMMENT=?, VERSION_SEQ_NUM=?, VERSION_LATEST=?, TRADE_HEADER_KEY=?, TRADE_SUMMARY=?, MODIFIED=?, UNDERLYING_DEF_ID1=?, UNDERLYING_DEF_ID2=?, ACTIVITY_STATUS=?, ORIGINATING_TRADE_ID=?, TERMS_CHANGED=?, DTS_SYMBOL=?, TRADE_TEMPLATE_ID_ID=?, TRADE_TEMPLATE_ID_NAME=?, TEMPLATE_USER_NAME=?, CASCADE_STATUS_ID=? where TRADE_KEY=?


Debug level Hibernate log excerpt:

2007-04-17 23:17:12,760 DEBUG [org.hibernate.impl.SessionImpl] - opened session at timestamp: 11768662326
2007-04-17 23:17:12,776 DEBUG [org.hibernate.transaction.JDBCTransaction] - begin
2007-04-17 23:17:12,776 DEBUG [org.hibernate.jdbc.ConnectionManager] - opening JDBC connection
2007-04-17 23:17:12,838 DEBUG [org.hibernate.transaction.JDBCTransaction] - current autocommit status: true
2007-04-17 23:17:12,838 DEBUG [org.hibernate.transaction.JDBCTransaction] - disabling autocommit
2007-04-17 23:17:12,838 DEBUG [org.hibernate.jdbc.JDBCContext] - after transaction begin
2007-04-17 23:17:12,869 INFO [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - Loading XML bean definitions from class path resource [org/springframework/jdbc/support/sql-error-codes.xml]
2007-04-17 23:17:12,901 INFO [org.springframework.jdbc.support.SQLErrorCodesFactory] - SQLErrorCodes loaded: [DB2, HSQL, MS-SQL, MySQL, Oracle, Informix, PostgreSQL, Sybase]
2007-04-17 23:17:12,916 DEBUG [org.hibernate.event.def.DefaultSaveOrUpdateEventListener] - saving transient instance
2007-04-17 23:17:12,916 DEBUG [org.hibernate.jdbc.AbstractBatcher] - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
2007-04-17 23:17:12,916 DEBUG [org.hibernate.SQL] - select S_TRD_TRADE.nextval from dual
Hibernate: select S_TRD_TRADE.nextval from dual
2007-04-17 23:17:12,916 DEBUG [org.hibernate.jdbc.AbstractBatcher] - preparing statement
2007-04-17 23:17:13,026 DEBUG [org.hibernate.id.SequenceGenerator] - Sequence identifier generated: 30
2007-04-17 23:17:13,026 DEBUG [org.hibernate.jdbc.AbstractBatcher] - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
2007-04-17 23:17:13,026 DEBUG [org.hibernate.jdbc.AbstractBatcher] - closing statement
2007-04-17 23:17:13,026 DEBUG [org.hibernate.event.def.AbstractSaveEventListener] - generated identifier: 30, using strategy: org.hibernate.id.SequenceGenerator
2007-04-17 23:17:13,026 DEBUG [org.hibernate.event.def.AbstractSaveEventListener] - saving [com.sknt.picasso.trade.Trade#30]
2007-04-17 23:17:13,026 DEBUG [org.hibernate.engine.Cascade] - processing cascade ACTION_SAVE_UPDATE for: com.sknt.picasso.trade.Trade
2007-04-17 23:17:13,026 DEBUG [org.hibernate.engine.CascadingAction] - cascading to saveOrUpdate: com.sknt.picasso.trade.TradeHeader
2007-04-17 23:17:13,041 DEBUG [org.hibernate.engine.IdentifierValue] - id unsaved-value: 0
2007-04-17 23:17:13,041 DEBUG [org.hibernate.event.def.AbstractSaveEventListener] - transient instance of: com.sknt.picasso.trade.TradeHeader
2007-04-17 23:17:13,041 DEBUG [org.hibernate.event.def.DefaultSaveOrUpdateEventListener] - saving transient instance
2007-04-17 23:17:13,041 DEBUG [org.hibernate.jdbc.AbstractBatcher] - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
2007-04-17 23:17:13,041 DEBUG [org.hibernate.SQL] - select S_TRD_TRADE_HEADER.nextval from dual
Hibernate: select S_TRD_TRADE_HEADER.nextval from dual
2007-04-17 23:17:13,041 DEBUG [org.hibernate.jdbc.AbstractBatcher] - preparing statement
2007-04-17 23:17:13,041 DEBUG [org.hibernate.id.SequenceGenerator] - Sequence identifier generated: 30
2007-04-17 23:17:13,041 DEBUG [org.hibernate.jdbc.AbstractBatcher] - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
2007-04-17 23:17:13,041 DEBUG [org.hibernate.jdbc.AbstractBatcher] - closing statement
2007-04-17 23:17:13,041 DEBUG [org.hibernate.event.def.AbstractSaveEventListener] - generated identifier: 30, using strategy: org.hibernate.id.SequenceGenerator
2007-04-17 23:17:13,041 DEBUG [org.hibernate.event.def.AbstractSaveEventListener] - saving [com.sknt.picasso.trade.OTCTradeHeader#30]
2007-04-17 23:17:13,041 DEBUG [org.hibernate.engine.Cascade] - processing cascade ACTION_SAVE_UPDATE for: com.sknt.picasso.trade.OTCTradeHeader
2007-04-17 23:17:13,041 DEBUG [org.hibernate.engine.Cascade] - done processing cascade ACTION_SAVE_UPDATE for: com.sknt.picasso.trade.OTCTradeHeader
2007-04-17 23:17:13,057 DEBUG [org.hibernate.event.def.WrapVisitor] - Wrapped collection in role: com.sknt.picasso.trade.TradeHeader.tradesInDealList
2007-04-17 23:17:13,057 DEBUG [org.hibernate.transaction.JDBCTransaction] - commit
2007-04-17 23:17:13,057 DEBUG [org.hibernate.impl.SessionImpl] - automatically flushing session
2007-04-17 23:17:13,057 DEBUG [org.hibernate.event.def.AbstractFlushingEventListener] - flushing session
2007-04-17 23:17:13,057 DEBUG [org.hibernate.event.def.AbstractFlushingEventListener] - processing flush-time cascades
2007-04-17 23:17:13,057 DEBUG [org.hibernate.engine.Cascade] - processing cascade ACTION_SAVE_UPDATE for: com.sknt.picasso.trade.Trade
2007-04-17 23:17:13,057 DEBUG [org.hibernate.engine.Cascade] - cascade ACTION_SAVE_UPDATE for collection: com.sknt.picasso.trade.Trade.versionID.statusList
2007-04-17 23:17:13,057 DEBUG [org.hibernate.engine.Cascade] - done cascade ACTION_SAVE_UPDATE for collection: com.sknt.picasso.trade.Trade.versionID.statusList
2007-04-17 23:17:13,057 DEBUG [org.hibernate.engine.CascadingAction] - cascading to saveOrUpdate: com.sknt.picasso.trade.TradeHeader
2007-04-17 23:17:13,057 DEBUG [org.hibernate.event.def.AbstractSaveEventListener] - persistent instance of: com.sknt.picasso.trade.TradeHeader
2007-04-17 23:17:13,057 DEBUG [org.hibernate.event.def.DefaultSaveOrUpdateEventListener] - ignoring persistent instance
2007-04-17 23:17:13,057 DEBUG [org.hibernate.event.def.DefaultSaveOrUpdateEventListener] - object already associated with session: [com.sknt.picasso.trade.OTCTradeHeader#30]
2007-04-17 23:17:13,057 DEBUG [org.hibernate.engine.Cascade] - done processing cascade ACTION_SAVE_UPDATE for: com.sknt.picasso.trade.Trade
2007-04-17 23:17:13,057 DEBUG [org.hibernate.engine.Cascade] - processing cascade ACTION_SAVE_UPDATE for: com.sknt.picasso.trade.OTCTradeHeader
2007-04-17 23:17:13,057 DEBUG [org.hibernate.engine.Cascade] - cascade ACTION_SAVE_UPDATE for collection: com.sknt.picasso.trade.TradeHeader.tradesInDealList
2007-04-17 23:17:13,072 DEBUG [org.hibernate.engine.Cascade] - done cascade ACTION_SAVE_UPDATE for collection: com.sknt.picasso.trade.TradeHeader.tradesInDealList
2007-04-17 23:17:13,072 DEBUG [org.hibernate.engine.Cascade] - done processing cascade ACTION_SAVE_UPDATE for: com.sknt.picasso.trade.OTCTradeHeader
2007-04-17 23:17:13,072 DEBUG [org.hibernate.event.def.AbstractFlushingEventListener] - dirty checking collections
2007-04-17 23:17:13,072 DEBUG [org.hibernate.event.def.AbstractFlushingEventListener] - Flushing entities and processing referenced collections
2007-04-17 23:17:13,072 DEBUG [org.hibernate.event.def.WrapVisitor] - Wrapped collection in role: com.sknt.picasso.trade.Trade.versionID.statusList
2007-04-17 23:17:13,072 DEBUG [org.hibernate.event.def.WrapVisitor] - Wrapped collection in role: com.sknt.picasso.trade.Trade.violatedRestrictionsList
2007-04-17 23:17:13,072 DEBUG [org.hibernate.event.def.WrapVisitor] - Wrapped collection in role: com.sknt.picasso.trade.Trade.cascadeResultTradeIDs
2007-04-17 23:17:13,072 DEBUG [org.hibernate.event.def.WrapVisitor] - Wrapped collection in role: com.sknt.picasso.trade.Trade.tradeReferenceMaps.referencesMap
2007-04-17 23:17:13,072 DEBUG [org.hibernate.event.def.WrapVisitor] - Wrapped collection in role: com.sknt.picasso.trade.Trade.tradeReferenceMaps.customReferencesMap
2007-04-17 23:17:13,072 DEBUG [org.hibernate.event.def.DefaultFlushEntityEventListener] - Updating entity: [com.sknt.picasso.trade.Trade#30]
2007-04-17 23:17:13,072 DEBUG [org.hibernate.engine.Collections] - Collection found: [com.sknt.picasso.trade.Trade.versionID.statusList#30], was: [<unreferenced>] (initialized)
2007-04-17 23:17:13,072 DEBUG [org.hibernate.engine.Collections] - Collection found: [com.sknt.picasso.trade.Trade.violatedRestrictionsList#30], was: [<unreferenced>] (initialized)
2007-04-17 23:17:13,088 DEBUG [org.hibernate.engine.Collections] - Collection found: [com.sknt.picasso.trade.Trade.cascadeResultTradeIDs#30], was: [<unreferenced>] (initialized)
2007-04-17 23:17:13,088 DEBUG [org.hibernate.engine.Collections] - Collection found: [com.sknt.picasso.trade.Trade.tradeReferenceMaps.referencesMap#30], was: [<unreferenced>] (initialized)
2007-04-17 23:17:13,088 DEBUG [org.hibernate.engine.Collections] - Collection found: [com.sknt.picasso.trade.Trade.tradeReferenceMaps.customReferencesMap#30], was: [<unreferenced>] (initialized)
2007-04-17 23:17:13,088 DEBUG [org.hibernate.event.def.DefaultFlushEntityEventListener] - Updating entity: [com.sknt.picasso.trade.OTCTradeHeader#30]
2007-04-17 23:17:13,088 DEBUG [org.hibernate.engine.Collections] - Collection found: [com.sknt.picasso.trade.TradeHeader.tradesInDealList#30], was: [<unreferenced>] (initialized)
2007-04-17 23:17:13,088 DEBUG [org.hibernate.event.def.AbstractFlushingEventListener] - Processing unreferenced collections
2007-04-17 23:17:13,088 DEBUG [org.hibernate.event.def.AbstractFlushingEventListener] - Scheduling collection removes/(re)creates/updates
2007-04-17 23:17:13,088 DEBUG [org.hibernate.event.def.AbstractFlushingEventListener] - Flushed: 0 insertions, 2 updates, 0 deletions to 2 objects
2007-04-17 23:17:13,088 DEBUG [org.hibernate.event.def.AbstractFlushingEventListener] - Flushed: 6 (re)creations, 0 updates, 0 removals to 6 collections
2007-04-17 23:17:13,088 DEBUG [org.hibernate.event.def.AbstractFlushingEventListener] - executing flush
2007-04-17 23:17:13,088 DEBUG [org.hibernate.jdbc.ConnectionManager] - registering flush begin
2007-04-17 23:17:13,088 DEBUG [org.hibernate.persister.entity.AbstractEntityPersister] - Updating entity: [com.sknt.picasso.trade.Trade#30]
2007-04-17 23:17:13,088 DEBUG [org.hibernate.jdbc.AbstractBatcher] - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
2007-04-17 23:17:13,088 DEBUG [org.hibernate.SQL] - update TRD_TRADE set TRADE_ID=?, VERSION_ID=?, VERSION_NUMBER=?, VERSION_CREATOR=?, VERSION_COMMENT=?, VERSION_SEQ_NUM=?, VERSION_LATEST=?, TRADE_HEADER_KEY=?, TRADE_SUMMARY=?, MODIFIED=?, UNDERLYING_DEF_ID1=?, UNDERLYING_DEF_ID2=?, ACTIVITY_STATUS=?, ORIGINATING_TRADE_ID=?, TERMS_CHANGED=?, DTS_SYMBOL=?, TRADE_TEMPLATE_ID_ID=?, TRADE_TEMPLATE_ID_NAME=?, TEMPLATE_USER_NAME=?, CASCADE_STATUS_ID=? where TRADE_KEY=?
Hibernate: update TRD_TRADE set TRADE_ID=?, VERSION_ID=?, VERSION_NUMBER=?, VERSION_CREATOR=?, VERSION_COMMENT=?, VERSION_SEQ_NUM=?, VERSION_LATEST=?, TRADE_HEADER_KEY=?, TRADE_SUMMARY=?, MODIFIED=?, UNDERLYING_DEF_ID1=?, UNDERLYING_DEF_ID2=?, ACTIVITY_STATUS=?, ORIGINATING_TRADE_ID=?, TERMS_CHANGED=?, DTS_SYMBOL=?, TRADE_TEMPLATE_ID_ID=?, TRADE_TEMPLATE_ID_NAME=?, TEMPLATE_USER_NAME=?, CASCADE_STATUS_ID=? where TRADE_KEY=?
2007-04-17 23:17:13,088 DEBUG [org.hibernate.jdbc.AbstractBatcher] - preparing statement
2007-04-17 23:17:13,104 DEBUG [org.hibernate.persister.entity.AbstractEntityPersister] - Dehydrating entity: [com.sknt.picasso.trade.Trade#30]
2007-04-17 23:17:13,104 DEBUG [org.hibernate.type.LongType] - binding '12345' to parameter: 1
2007-04-17 23:17:13,104 DEBUG [org.hibernate.type.LongType] - binding '6789' to parameter: 2
2007-04-17 23:17:13,104 DEBUG [org.hibernate.type.IntegerType] - binding '1' to parameter: 3
2007-04-17 23:17:13,104 DEBUG [org.hibernate.type.StringType] - binding 'me' to parameter: 4
2007-04-17 23:17:13,104 DEBUG [org.hibernate.type.StringType] - binding 'haha' to parameter: 5
2007-04-17 23:17:13,104 DEBUG [org.hibernate.type.LongType] - binding '12' to parameter: 6
2007-04-17 23:17:13,104 DEBUG [org.hibernate.type.BooleanType] - binding 'false' to parameter: 7
2007-04-17 23:17:13,104 DEBUG [org.hibernate.type.LongType] - binding '30' to parameter: 8
2007-04-17 23:17:13,135 DEBUG [org.hibernate.type.BooleanType] - binding 'true' to parameter: 10
2007-04-17 23:17:13,135 DEBUG [org.hibernate.type.LongType] - binding '11' to parameter: 11
2007-04-17 23:17:13,135 DEBUG [org.hibernate.type.LongType] - binding '22' to parameter: 12
2007-04-17 23:17:13,135 DEBUG [org.hibernate.type.IntegerType] - binding '33' to parameter: 13
2007-04-17 23:17:13,135 DEBUG [org.hibernate.type.LongType] - binding '11111' to parameter: 14
2007-04-17 23:17:13,135 DEBUG [org.hibernate.type.BooleanType] - binding 'false' to parameter: 15
2007-04-17 23:17:13,135 DEBUG [org.hibernate.type.StringType] - binding 'some symbol' to parameter: 16
2007-04-17 23:17:13,135 DEBUG [org.hibernate.type.LongType] - binding '1' to parameter: 17
2007-04-17 23:17:13,135 DEBUG [org.hibernate.type.StringType] - binding 'tt' to parameter: 18
2007-04-17 23:17:13,135 DEBUG [org.hibernate.type.StringType] - binding 'me' to parameter: 19
2007-04-17 23:17:13,135 DEBUG [org.hibernate.type.IntegerType] - binding '23' to parameter: 20
2007-04-17 23:17:13,135 DEBUG [org.hibernate.type.LongType] - binding '30' to parameter: 21
2007-04-17 23:17:13,151 ERROR [org.hibernate.event.def.AbstractFlushingEventListener] - Could not synchronize database state with session

Problems with Session and transaction handling?

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


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

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.