I'm having the exact same problem (symptom) as described above and it is driving me nuts.
For me, it all boils down to the second SQL (set AC_ID=null)... Why is this being generated?
In my case, I should not be making any references to the Chart class.
I have cascade set to "none". Note, I actually want cascading but set it to none in an attempt to isolate the problem.
Code:
11:03:49,761 INFO [STDOUT] Hibernate: update actype set VER=?, TYPE=?, SUFFIX=?, MODEL=? where ID=? and VER=?
11:03:49,777 INFO [STDOUT] Hibernate: update chart set AC_ID=null where AC_ID=?
Environment:
Windows XP Pro
JBoss: 4.0.2 with EJB 3.0
Spring: 1.2.1
MySql: 4.1 Connector: 3.1.8
Hibernate: 3.0.5
Architecture 3 tier:
Web Server
App Server
DB Server
DB access via EJB and Transfer Objects
Scenario:
Web tier calls EJB to update actype object. Cascade set to "false" for all child objects and collections.
Hibernate creates correct update SQL:
Code:
11:03:49,761 INFO [STDOUT] Hibernate: update actype set VER=?, TYPE=?, SUFFIX=?, MODEL=? where ID=? and VER=?
Then throws in this extra one that triggers the exception:
Code:
11:03:49,777 INFO [STDOUT] Hibernate: update chart set AC_ID=null where AC_ID=?
Actype.hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping
default-cascade="none"
default-access="property"
default-lazy="true"
package="com.eflight.eloadsheet.domainobjects">
<class name="Actype" table="actype">
<meta attribute="class-description">
Aircraft Type
@author Paul Baker
</meta>
<id name="id" type="long">
<meta attribute="field-description">
ID field.
</meta>
<meta attribute="scope-set">public</meta>
<meta attribute="scope-get">public</meta>
<meta attribute="use-in-tostring">true</meta>
<column name="ID" precision="20" />
<generator class="native" />
</id>
<version name="ver" column="VER" type="long"/>
<property name="type" type="string">
<meta attribute="use-in-tostring">true</meta>
<column name="TYPE" length="3" not-null="true" />
</property>
<property name="suffix" type="string">
<meta attribute="use-in-tostring">true</meta>
<column name="SUFFIX" length="3" not-null="true" />
</property>
<property name="model" type="string">
<meta attribute="use-in-tostring">true</meta>
<column name="MODEL" length="60" not-null="true" />
</property>
<one-to-one name="OptLoadsheet" class="OptLoadsheet" cascade="none" constrained="true"/>
<one-to-one name="WbType" class="WbType" cascade="none" constrained="true"/>
<set name="cgCharts" lazy="true" cascade="none">
<key column="AC_ID" />
<one-to-many class="Chart" />
</set>
</class>
</hibernate-mapping>
DAO Class:
Code:
/**
* @see com.dygtig.domain.dao.ActypeDao#update()
*/
public void update(Actype actype) {
this.getHibernateTemplate().update(actype);
this.getHibernateTemplate().flush();
return;
}
I have spent several hours trying to track this problem down... I will gladly supply any additional information...
Thanks in advance... Paul
Here is a bit more of the trace:
Code:
11:03:45,793 INFO [DefaultListableBeanFactory] Creating shared instance of singleton bean 'transactionManager'
11:03:45,793 INFO [JtaTransactionManager] Using JTA UserTransaction: org.jboss.tm.usertx.client.ServerVMClientUserTransaction@ea2352
11:03:45,793 INFO [JtaTransactionManager] Using JTA TransactionManager: org.jboss.tm.TxManager@1b505fe
11:03:45,793 INFO [DefaultListableBeanFactory] Creating shared instance of singleton bean 'actypeService'
11:03:49,761 INFO [STDOUT] Hibernate: update actype set VER=?, TYPE=?, SUFFIX=?, MODEL=? where ID=? and VER=?
11:03:49,777 INFO [STDOUT] Hibernate: update chart set AC_ID=null where AC_ID=?
11:03:49,918 WARN [JDBCExceptionReporter] SQL Error: 1216, SQLState: 23000
11:03:49,918 ERROR [JDBCExceptionReporter] Cannot add or update a child row: a foreign key constraint fails
11:03:49,933 ERROR [AbstractFlushingEventListener] Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:74)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:181)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:226)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:138)
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:730)
at org.springframework.orm.hibernate3.HibernateTemplate$27.doInHibernate(HibernateTemplate.java:713)
at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:312)
at org.springframework.orm.hibernate3.HibernateTemplate.flush(HibernateTemplate.java:711)
at com.eflight.eloadsheet.daos.hibernate.ActypeDaoHibernateImpl.update(ActypeDaoHibernateImpl.java:57)
at com.eflight.eloadsheet.service.impl.ActypeServiceImpl.update(ActypeServiceImpl.java:87)
at com.eflight.eloadsheet.actypecrudejb.ActypeCrudBean.update(ActypeCrudBean.java:67)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)