I don't understand why I get this exception thrown at me:
Code:
Caused by: java.sql.SQLException: UPDATE statement conflicted with COLUMN FOREIGN KEY constraint 'FK_dr_deviation_report_sc_complaints'. The conflict occurred in database 'novaq', table 'Tmp_sc_complaints', column 'id'.
I use Hibernate 2.1.2 with jTDS' driver to connect to Microsoft SQL Server 2000. The Dialect I'm using is this:
Code:
public class WorkaroundDialect extends SQLServerDialect {
/** Creates a new instance of WorkaroundDialect */
public WorkaroundDialect() {
super();
}
/**
* Returns <code>null</code> since the automatic fetching is malfunctional.
*/
public String appendIdentitySelectToInsert(String insertSQL) {
return null;
}
}
The mappings are:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping package="se.novaseptic.novaq.claims>
<class name="SupplierComplaint" table="sc_complaints">
<id name="id" column="id" type="int">
<generator class="identity"/>
</id>
<property name="serial" not-null="true"/>
<property name="message"/>
<property name="start" not-null="true" />
<property name="immediateSolution" column="immediate_solution"/>
<property name="temporarySolution" column="temporary_solution" />
<property name="investigation" column="investigation"/>
<property name="permanentSolution" column="permanent_solution" />
<!-- supplier may be null during creation -->
<many-to-one name="supplier" column="supplier" not-null="true" />
<many-to-one name="assignee" column="assignee" not-null="true"/>
<many-to-one name="state" column="current_state" not-null="true" />
<set name="stateChanges" inverse="true" cascade="all" order-by="stamp ASC">
<key column="complaint" />
<one-to-many class="SupplierComplaintStateChange" />
</set>
<set name="deviationReports" inverse="true" cascade="all"
order-by="deviation_type, deviation_subtype" ><!-- batch-size="0" -->
<key column="supplier_complaint" />
<one-to-many class="DeviationReport" />
</set>
</class>
</hibernate-mapping>
and
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping package="se.novaseptic.novaq.claims">
<class name="DeviationReport" table="dr_deviation_report">
<id name="id" column="id" type="int">
<generator class="identity"/>
</id>
<property name="serial"/>
<property name="stamp"/>
<property name="note"/>
<many-to-one name="status"/>
<many-to-one name="operator"/>
<many-to-one name="supplierComplaint" column="supplier_complaint"/>
<many-to-one name="incomingGoods" column="incoming_goods"/>
<many-to-one name="deviationType" column="deviation_type"/>
<many-to-one name="deviationSubType" column="deviation_subtype"/>
<set name="deviations" inverse="true" cascade="all" order-by="id">
<key column="deviation_report" />
<one-to-many class="Deviation" />
</set>
</class>
</hibernate-mapping>
The code that is executed is:
Code:
/**
* Creates a new Supplier Complaint including the specified deviation reports. The specified
* user will be assigned the complaint and it's state will be set to 1.
*
* @param user the user that will handle this complaint
* @param deviationReports array of ids of the reports to include
*/
public static SupplierComplaint newSupplierComplaint(User user, Integer[] deviationReports)
throws HibernateException
{
Session hibernate = HibernateFilter.getSession();
Transaction tx = hibernate.beginTransaction();
try {
SupplierComplaint complaint = new SupplierComplaint();
// FIXME: this is temporary, use SerialStrategy.getX()
String serial = "" + System.currentTimeMillis();
serial = "SC" + serial.substring(serial.length() - 4);
complaint.setAssignee(user);
complaint.setNewState(user, new SupplierComplaintState(SupplierComplaintState.ASSIGNED));
complaint.setStart(new java.sql.Timestamp(System.currentTimeMillis()));
complaint.setSerial(serial);
hibernate.save(complaint);
hibernate.flush();
// for each String in deviationReports, add the deviationreport to this complaint
// make sure all reports relate to the same Supplier
Set reports = new HashSet();
for (int i = 0; i < deviationReports.length; i++) {
DeviationReport dr = (DeviationReport)
hibernateLoad(DeviationReport.class, deviationReports[i]);
if (complaint.getSupplier() == null) {
complaint.setSupplier(dr.getIncomingGoods().getSupplier());
} else {
if (complaint.getSupplier().getId() !=
dr.getIncomingGoods().getSupplier().getId())
{
throw new IllegalArgumentException(
"All Deviation Reports included in a Supplier Complaint " +
"must relate to the same supplier! The ones you specified does not.");
}
}
dr.setSupplierComplaint(complaint);
dr.setStatus(new DeviationReportStatus(
DeviationReportStatus.INCLUDED_IN_SUPPLIER_COMPLAINT));
hibernate.update(dr);
140 140 hibernate.flush();
reports.add(dr);
}
// update complaint.supplier
complaint.setDeviationReports(reports);
hibernate.update(complaint);
hibernate.flush();
tx.commit();
return complaint;
} catch (HibernateException he) {
HibernateFilter.rollback(tx);
throw he;
}
}
This is from the log4j-log file:
Code:
17:21:11,909 DEBUG SQL:237 - update sc_complaints set serial=?, message=?, start=?, immediate_solution=?, temporary_solution=?, investigation=?, permanent_solution=?, supplier=?, assignee=?, current_state=? where id=?
17:21:11,969 DEBUG SQL:237 - update users set employee_id=?, activated=?, description=?, email=?, first_name=?, last_name=?, password=?, username=? where id=?
17:21:12,032 DEBUG SQL:237 - update dr_deviation_report set serial=?, stamp=?, note=?, status=?, operator=?, supplier_complaint=?, incoming_goods=?, deviation_type=?, deviation_subtype=? where id=?
17:21:12,126 WARN JDBCExceptionReporter:38 - SQL Error: 547, SQLState: S1000
The "full" stack trace:
Code:
java.lang.Exception: net.sf.hibernate.JDBCException: could not update: [se.novaseptic.novaq.claims.DeviationReport#4]
at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:706)
at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:641)
at net.sf.hibernate.impl.ScheduledUpdate.execute(ScheduledUpdate.java:52)
at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2382)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2336)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2204)
at se.novaseptic.novaq.claims.ClaimsModelController.newSupplierComplaint(ClaimsModelController.java:140)
at se.novaseptic.novaq.claims.ClaimsWebController.makeSupplierComplaint(ClaimsWebController.java:111)
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:324)
at se.novaseptic.novaq.servlet.CTLServlet.dispatch(CTLServlet.java:57)
at se.novaseptic.novaq.servlet.MasterServlet.doGetOrPost(MasterServlet.java:130)
at se.novaseptic.novaq.servlet.CTLServlet.service(CTLServlet.java:24)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
at se.novaseptic.novaq.servlet.HibernateFilter.doFilter(HibernateFilter.java:78)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:213)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:256)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2422)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:171)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:163)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:199)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:828)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:700)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:584)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
at java.lang.Thread.run(Thread.java:534)
Caused by: net.sf.hibernate.JDBCException: could not update: [se.novaseptic.novaq.claims.DeviationReport#4]
... 48 more
Caused by: java.sql.SQLException: UPDATE statement conflicted with COLUMN FOREIGN KEY constraint 'FK_dr_deviation_report_sc_complaints'. The conflict occurred in database 'novaq', table 'Tmp_sc_complaints', column 'id'.
at net.sourceforge.jtds.jdbc.SqlMessage.toSQLException(SqlMessage.java:85)
at net.sourceforge.jtds.jdbc.SQLWarningChain.addOrReturn(SQLWarningChain.java:99)
at net.sourceforge.jtds.jdbc.TdsStatement.getMoreResults(TdsStatement.java:709)
at net.sourceforge.jtds.jdbc.TdsStatement.executeCallImpl(TdsStatement.java:231)
at net.sourceforge.jtds.jdbc.TdsStatement.internalExecuteCall(TdsStatement.java:214)
at net.sourceforge.jtds.jdbc.PreparedStatement_base.execute(PreparedStatement_base.java:180)
at net.sourceforge.jtds.jdbc.PreparedStatement_base.executeUpdate(PreparedStatement_base.java:235)
at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:207)
at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:22)
at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:688)
... 47 more
I've tried saving and updating in various orders but I can't get it to work. Any ideas on what to do?
Thanks in advance, Fredrik