-->
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.  [ 2 posts ] 
Author Message
 Post subject: Load / Delete in different transactions
PostPosted: Fri Dec 22, 2006 6:16 am 
Newbie

Joined: Wed Oct 11, 2006 11:32 am
Posts: 18
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 !


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 22, 2006 11:00 am 
Beginner
Beginner

Joined: Thu Sep 22, 2005 10:48 am
Posts: 30
Location: Rio de Janeiro, Brazil
Try insert inverse="true" in many-to-one

Code:
<many-to-one name="VarReportSetupId" class="VarReportSetup" column="VarReportSetupId" not-null="true" inverse="true" />

_________________
Please don't forget to rateME!


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

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.