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.  [ 3 posts ] 
Author Message
 Post subject: Cascade "all-delete-orphan" and unique index
PostPosted: Tue Oct 07, 2008 3:57 am 
Beginner
Beginner

Joined: Thu Oct 25, 2007 3:45 am
Posts: 22
Hi,

We have two classes (Client and ClientAccount) with one-to-many relation:

<bag name="ClientAccounts"
table="ClientAccount"
cascade="all-delete-orphan"
inverse="false"
lazy="true">
<key column="ClientID" />
<one-to-many class="Common.ClientAccount, Common"/>
</bag>

There is in the database a unique index, including several columns of the ClientAccount table.

-----------------------------------------------------------------

There are troubles in a case like this:

From the database is loaded a Client with one ClientAccount. The user removes this ClientAccount and the code for that is:

client.ClientAccounts.Remove(clientAccount1);

Then he adds a new ClientAccount:

client.ClientAccounts.Add(clientAccount2);

We try to save the Client object:

session.SaveOrUpdateCopy(client);

The exception is received: "Cannot insert duplicate key row in object 'dbo.ClientAccount' with unique index 'IX_ClientAccount'.\r\nThe statement has been terminated."

-----------------------------------------------------------------

Obviously NHibernate first tries to add the object clientAccount2 (before removing clientAccount1) and it violates the unique index.

Please, suggest a solution.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 08, 2008 10:20 am 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
You have to delete clientAccount1 itself, in addition to removing it from client.

_________________
Gonzalo Díaz


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 09, 2008 3:30 am 
Beginner
Beginner

Joined: Thu Oct 25, 2007 3:45 am
Posts: 22
gonzao_diaz wrote:
You have to delete clientAccount1 itself, in addition to removing it from client.


Thank you for helping. For now I removed the unique index and made only interface verification. Of course, it is not the best solution...

I'll think for using your method...


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.