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
...