Hello people !
i got a problem with a simple one to many association.
i have a first table 'VarReportSetup' and a second one, 'VarReportAggregationTerm', which links the first one with a foreign key.
I have a VarReportSetup associated with two VarReportAggregationTerm. I load it from the database in a first transaction. I want to delete this bunch of objects with a cascading delete in a second transaction but this doesn't work, though those operations work fine in a single transaction.
Here is the mapping of the first table :
Code:
<class name="VarReportSetup" table="VarReportSetup">
<...>
<set name="aggregationTermCollection" table="VarReportAggregationTerm" cascade="all">
<key column="VarReportSetupId" />
<one-to-many class="VarReportAggregationTerm"/>
</set>
</class>
Here is the mapping of the second table
Code:
<class name="VarReportAggregationTerm" table="VarReportAggregationTerm">
<...>
<many-to-one name="VarReportSetupId" class="VarReportSetup" column="VarReportSetupId" not-null="true"/>
</class>
I first load the data :
Code:
VarReportSetup setup = null;
Transaction tx = null;
try {
tx = session.beginTransaction();
setup = (VarReportSetup) session
.createQuery("select p from VarReportSetup p left join fetch p.filterCollection where p.Id = :pid")
.setParameter("pid", id)
.uniqueResult(); // Eager fetch the collection so we can use it detached
tx.commit();
} catch (Exception e) {
e.printStackTrace();
if (tx != null) tx.rollback();
}
and then the delete
Code:
Transaction tx2 = null;
try {
tx2 = session.beginTransaction();
session.delete(setup);
tx2.commit();
} catch (Exception e) {
e.printStackTrace();
if (tx2 != null) tx2.rollback();
}
It does not work either with a persist before calling the delete.
Finally the stack trace ;)
Code:
Hibernate: update VarReportAggregationTerm set VarReportSetupId=null where VarReportSetupId=?
09:34:06,155 WARN JDBCExceptionReporter:71 - SQL Error: 233, SQLState: 23000
09:34:06,155 ERROR JDBCExceptionReporter:72 - The column VarReportSetupId in table VarReportAggregationTerm does not allow null values.
09:34:06,171 ERROR AbstractFlushingEventListener:301 - Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: could not delete collection: [com.reuters.hibernate.test.VarReportSetup.aggregationTermCollection#7669695436a844ce6dbee94b]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.persister.collection.AbstractCollectionPersister.remove(AbstractCollectionPersister.java:1071)
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:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at com.reuters.hibernate.test.api.ProfilingLocaleUserApi.main(ProfilingLocaleUserApi.java:100)
Caused by: java.sql.SQLException: The column VarReportSetupId in table VarReportAggregationTerm does not allow null values.
at net.sourceforge.jtds.jdbc.SQLDiagnostic.addDiagnostic(SQLDiagnostic.java:365)
at net.sourceforge.jtds.jdbc.TdsCore.tdsErrorToken(TdsCore.java:2781)
at net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:2224)
at net.sourceforge.jtds.jdbc.TdsCore.getMoreResults(TdsCore.java:633)
at net.sourceforge.jtds.jdbc.JtdsStatement.processResults(JtdsStatement.java:525)
at net.sourceforge.jtds.jdbc.JtdsStatement.executeSQL(JtdsStatement.java:487)
at net.sourceforge.jtds.jdbc.JtdsPreparedStatement.executeUpdate(JtdsPreparedStatement.java:421)
at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:23)
at org.hibernate.persister.collection.AbstractCollectionPersister.remove(AbstractCollectionPersister.java:1048)
... 10 more
Thanks a lot !