I have two entities in my database, Module and Command, that are in a one-to-many relationship, as a Module can have multiple Commands assigned to it. Moreover, a Command is only meaningful when assigned to a Module. So, I would like to define a cascading policy such that the deletion of a Module should cause all the related Commands to be implicitly deleted.
I'm using Hibernate and I have a requirement to define the entities through xml mapping files. I'm using the following configuration:
In Module.hbm.xml
....
<list name="pendingCommands" table="COMMANDS" inverse="true" lazy="false" cascade="all">
<key column="MODULE_ID" />
<list-index column="COMMAND_IDX" />
<one-to-many class="Command" />
</list>
....
In Command.hbm.xml
....
<many-to-one name="module" class="Module">
<column name="MODULE_ID" not-null="true" />
</many-to-one>
....
This works on hsqldb: deleting a Module object via org.hibernate.Session.delete always succeeds and, if the Module has related Command objects, hibernate deletes them first.
However, the same seemed to fail on MS SQL, where I got an error like this:
The DELETE statement conflicted with the REFERENCE constraint ""FKxxx"". The conflict occurred in database ""yyy"", table ""dbo.COMMANDS"", column 'MODULE_ID'.
Is it really an implementation-dependent behavior? Is there something I should change in my configuration?
Thank you in advance for any tip.