Hello all.
I am having trouble with a one-to-many mapping, but it is different from all of the examples and other questions that I have seen. This is because in my mapping the unidirection is from the one side, not the many side (don't know if that makes sense, but I don't have a set/list/bag/collection involved).
I have an Event class which has one Contact. The Contacts are reusable with different Events, but only the Event know who its Contact is.
I have mapped this as follows:
Code:
<class name="Event" table="Event">
<many-to-one name="Contact"
column="ContactID"
class="Contact"/>
</class>
There is no mapping in the Project mapping file relating to Event.
The problem that I am having is if I have associated a Project to an Event, and then I delete the Project I get the ever so predictable foreign key constraint violation.
This is not intended to be a parent-child relationship, but rather is just a reference. In other words, deleting an Event should not cascade to the Project. Projects are independent from Events and should only be deleted when they are explicitly deleted, but when they are deleted, I want all references to the deleted Project to be removed from all Events.
Is there a way to modify my mapping to give me the behavior I desire? I have tried various cascade models, and setting not-null="false".
Thanks,
Fang
PS. If it helps, here is my stack trace:
Code:
Exception in thread "main" org.hibernate.exception.ConstraintViolationException: could not delete: [Contact#01dd861e-ac18-6b7a-7811-9588bee26f40]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2537)
at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2693)
at org.hibernate.action.EntityDeleteAction.execute(EntityDeleteAction.java:74)
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:144)
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 TestMain.cascadeDeleteTesting(TestMain.java:122)
at TestMain.main(TestMain.java:104)
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: The DELETE statement conflicted with the REFERENCE constraint "FK403827A78AC8D0B". The conflict occurred in database "TestWG", table "dbo.Event", column 'ContactID'.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(Unknown Source)
at com.microsoft.sqlserver.jdbc.IOBuffer.processPackets(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.sendExecute(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.doExecuteUpdate(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeUpdate(Unknown Source)
at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:23)
at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2516)
... 12 more