Ok, I have two classes Dealer(parent) and a DealerMultiplier(child). Creating the Dealer works just fine and reading Dealers in from the DB works just fine. However, If I create a Dealer then add some DealerMultiplier's to the Dealer then do an update on the Dealer it fails.
The interesting thing is, in the generated SQL in the log you see a insert for each DealerMultiplier then an update to the Dealer table (why I don't at this point but - ok) and then finally a update which sets the FK value (DEALER_ID) in DealerMultiplier to NULL???
This last SQL statement that is being generated I believe is the issue however, I've got no feak'n idea why Hibernate is doing this or how to stop it.
Hibernate version: 3.1
Name and version of the database you are using: MySQL 5
Mapping documents:
Dealer.hbm.xml
Code:
<hibernate-mapping>
<class name="spif.biz.Dealer" table="DEALER" catalog="SPIFF" >
<cache usage="read-write" include="all" />
<id name="dealerId" type="integer">
<column name="DEALER_ID"/>
<generator class="identity" />
</id>
<property name="parentId" type="integer">
<column name="PARENT_ID" />
</property>
<property name="dealerCode" type="string">
<column name="DEALER_CODE" length="8" not-null="true"/>
</property>
<property name="dealerName" type="string">
<column name="DEALER_NAME" length="30" not-null="true"/>
</property>
<property name="address1" type="string">
<column name="ADDRESS1" length="60" not-null="true"/>
</property>
<property name="address2" type="string">
<column name="ADDRESS2" length="60"/>
</property>
<property name="suiteApt" type="string">
<column name="SUITE_APT" length="30"/>
</property>
<property name="city" type="string">
<column name="CITY" length="40" not-null="true"/>
</property>
<property name="stateCode" type="string">
<column name="STATE_CODE" length="3" not-null="true"/>
</property>
<property name="zip" type="string">
<column name="ZIP" length="20" not-null="true"/>
</property>
<property name="phone" type="string">
<column name="PHONE" length="20" />
</property>
<property name="fax" type="string">
<column name="FAX" length="20" />
</property>
<property name="email" type="string">
<column name="EMAIL" length="40" />
</property>
<property name="contact" type="string">
<column name="CONTACT" length="40" />
</property>
<property name="active" type="boolean">
<column name="ACTIVE"/>
</property>
<property name="project" type="boolean">
<column name="PROJECT_FLAG"/>
</property>
<set name="dealerMultipliers" lazy="false" fetch="select" cascade="all" >
<key column="DEALER_ID" />
<one-to-many class="spif.biz.DealerMultiplier" />
</set>
<set name="projects" lazy="false" fetch="select" cascade="all" >
<key column="PARENT_ID" />
<one-to-many class="spif.biz.Dealer" />
</set>
</class>
</hibernate-mapping>
and DealerMultiipler.hbm.xml
Code:
<hibernate-mapping>
<class name="spif.biz.DealerMultiplier" table="DEALER_MULTIPLIER" catalog="SPIFF">
<id name="dealerMultiplierId" type="integer">
<column name="DEALER_MULTIPLIER_ID" not-null="true" />
<generator class="identity" />
</id>
<many-to-one name="productLine" class="spif.biz.ProductLine"
fetch="select" lazy="false" not-null="true">
<column name="PRODUCT_LINE_ID" not-null="true" />
</many-to-one>
<property name="multiplier" type="float">
<column name="MULTIPLIER" precision="12" scale="0" not-null="true"/>
</property>
<property name="dealerId" type="integer" not-null="true">
<column name="DEALER_ID" precision="12" scale="0"
not-null="true" />
</property>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():In sudo-code:
Code:
get a session
new a Dealer instance and fill out the attribs
do session.create(dealer)
commit
create a couple DealerMultipliers and add them to the Dealer
get a session
so session.update(dealer)
commit
BAM!
Full stack trace of any exception that occurs:Code:
22:55:57,044 WARN JDBCExceptionReporter:71 - SQL Error: 0, SQLState: 01004
22:55:57,047 ERROR JDBCExceptionReporter:72 - Data truncation: Column was set to data type implicit default; NULL supplied for NOT NULL column 'DEALER_ID' at row 1
22:55:57,063 ERROR AbstractFlushingEventListener:300 - Could not synchronize database state with session
org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:202)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:91)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:86)
at org.hibernate.jdbc.AbstractBatcher.prepareBatchStatement(AbstractBatcher.java:171)
at org.hibernate.persister.collection.AbstractCollectionPersister.remove(AbstractCollectionPersister.java:965)
at org.hibernate.action.CollectionRemoveAction.execute(CollectionRemoveAction.java:28)
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:141)
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 spif.bizfactory.DealerFactory.updateDealer(DealerFactory.java:410)
at spif.bizfactory.DealerFactory.initMultipliers(DealerFactory.java:643)
at spif.bizfactory.testing.MultiplierTest.process(MultiplierTest.java:40)
at spif.bizfactory.testing.FactoryTester.runTest(FactoryTester.java:64)
at spif.bizfactory.testing.FactoryTester.processTests(FactoryTester.java:37)
at spif.bizfactory.testing.FactoryTester.main(FactoryTester.java:85)
Caused by: java.sql.BatchUpdateException: Data truncation: Column was set to data type implicit default; NULL supplied for NOT NULL column 'DEALER_ID' at row 1
at com.mysql.jdbc.ServerPreparedStatement.executeBatch(ServerPreparedStatement.java:665)
at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeBatch(NewProxyPreparedStatement.java:1723)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:58)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:195)
... 19 more
The generated SQL (show_sql=true):Code:
Hibernate:
insert
into
SPIFF.DEALER_MULTIPLIER
(PRODUCT_LINE_ID, MULTIPLIER, DEALER_ID)
values
(?, ?, ?)
Hibernate:
insert
into
SPIFF.DEALER_MULTIPLIER
(PRODUCT_LINE_ID, MULTIPLIER, DEALER_ID)
values
(?, ?, ?)
Hibernate:
update
SPIFF.DEALER
set
PARENT_ID=?,
DEALER_CODE=?,
DEALER_NAME=?,
ADDRESS1=?,
ADDRESS2=?,
SUITE_APT=?,
CITY=?,
STATE_CODE=?,
ZIP=?,
PHONE=?,
FAX=?,
EMAIL=?,
CONTACT=?,
ACTIVE=?,
PROJECT_FLAG=?
where
DEALER_ID=?
Hibernate: <-- What the freak is this one for?
update
SPIFF.DEALER_MULTIPLIER
set
DEALER_ID=null
where
DEALER_ID=?
[/b]