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.  [ 1 post ] 
Author Message
 Post subject: Many-to-many cascade
PostPosted: Mon Apr 19, 2010 6:17 am 
Regular
Regular

Joined: Wed Mar 10, 2010 4:48 am
Posts: 106
I have a many-to-many relation between two objects. If one of these objects is deleted, the relations need to be deleted as well, but the other object related to should remain. How can I do this?

I have the following mappings:
Code:
<class name="data.Position" table="positions">
      <id name="id" column="id" length="31">
         <generator class="assigned" />
      </id>

      <property name="name" column="name" length="20" not-null="true" />
      <property name="description" column="description" />

      <set name="rules" table="positionRules" cascade="delete">
         <key column="position" />
         <many-to-many class="data.Rule" column="rule" />
      </set>
Code:
<class name="data.Rule" table="rules">
      <id name="id" column="id" length="31">
         <generator class="assigned" />
      </id>

      <set name="positions" table="positionRules" inverse="true" cascade="delete">
         <key column="rule" />
         <many-to-many class="data.Position" column="position" />
      </set>
   </class>
Now when I delete a position Hibernates does try to delete the position, but an error appears, because the position is still part of the relation. Then it continous and deletes the relations.
So all relations are deleted, the object itself isn't and I get an error:
Quote:
WARN - (JDBCExceptionReporter.java:100) - SQL Error: 1048, SQLState: 23000
ERROR - (JDBCExceptionReporter.java:101) - Column 'rule' cannot be null
ERROR - (AbstractFlushingEventListener.java:324) - Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
...
Caused by: java.sql.BatchUpdateException: Column 'rule' cannot be null
at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:2015)
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1451)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
... 40 more
ERROR - (PositionsPanel.java:297) - Could not execute JDBC batch update
WARN - (JDBCExceptionReporter.java:100) - SQL Error: 1048, SQLState: 23000
ERROR - (JDBCExceptionReporter.java:101) - Column 'rule' cannot be null
Exception in thread "AWT-EventQueue-0" ERROR - (AbstractFlushingEventListener.java:324) - Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
...
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
...


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

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.