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.  [ 1 post ] 
Author Message
 Post subject: Unique key violation as insert occurs before update
PostPosted: Fri Oct 13, 2006 6:26 am 
Newbie

Joined: Mon Apr 03, 2006 5:12 pm
Posts: 15
Location: Glasgow, Scotland
NHibernate 1.0.2
MSDE 2000

Code:
I have a table similar to the following:
CREATE TABLE [MyTable] (
   [MyTablleId]    [int]    NOT NULL PRIMARY KEY NONCLUSTERED ,
   [MyParentTableId]  [int] NOT NULL ,
   [Col1] [varchar]    (100)      NOT NULL ,
   [Col2] [varchar]   (100)      NULL ,
   [Col3] [varchar]   (100)      NULL ,
   [Col4] [varchar]   (100)       NULL ,
   CONSTRAINT [FK_MyTable_MyParentTable] FOREIGN KEY
   (
      [MyParentTableId]
   ) REFERENCES [MyParentTable] (
      [MyParentTableId]
   )
)

// Index for performance
CREATE CLUSTERED INDEX [MyTable_SS1] ON [MyTable]
(MyParentTableId)
GO

// Business logic index
CREATE UNIQUE NONCLUSTERED INDEX [MyTable_SS2] ON [MyTable]
(MyParentTableId, Col1, Col2, Col3)
GO

So I have a foreign key to MyParentTable, an index on this column for performance and then a business logic index to ensure no duplicates exist with the same values for Col1, 2 and 3.

In my application I have a situation where the user enters what is initially a duplicate entry and then changes the existing entry so no duplicates occur.

I use session.SaveOrUpdate and NHibernate performs the insert first which causes a violation of the second index. It needs to perform the update first then the insert.

Is there something wrong in the way I am defining my table which is causing this or can I specify the order of inserts/updates?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.