-->
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: One-to-many, but different
PostPosted: Tue Apr 17, 2007 7:27 pm 
Newbie

Joined: Fri Mar 09, 2007 6:12 pm
Posts: 2
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


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 26, 2007 6:53 am 
Newbie

Joined: Thu Aug 25, 2005 10:04 am
Posts: 6
I have the same problem.
This is probably not possible with hibernate, but maybe someone can confirm this.

A common use case would be, for example, a persistent user of this forum with hundreds of persistent posts, which you definitely don't want to be loaded (not even lazily) during a log in, so you would use a finder.
But on deletion, I don't want to redundantly reuse the object hierarchy to write my one cascade delete. The database seems to know the constraints (hence the exception), so how could we use this in hibernate?


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.