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: Foreign key problems - how best to handle?
PostPosted: Thu Nov 20, 2008 1:20 pm 
Newbie

Joined: Tue May 15, 2007 3:53 pm
Posts: 9
Hibernate version:
1.2GA

I have been tasked with trying to port our working web app from Oracle to SQL Server 2005. This app is currently working and stable with NHibernate. To begin the migration, I used Microsoft's SSMA (SQL Server Migration Assitant) which did a good job of converting the schema and data to MSSQL, and once I had changed all the Oracle sequences to MSSQL identity columns, the entire app was once again working on the new db...

Except: now I am getting foreign key errors when deleting a record that I wasn't getting on the Oracle version.

For example, I have a Document class (which maps to a table called "Library"). Every document record can have a list of links to other "Associated Documents". This is done with a table called "r_library_library." The schema looks like this:

Code:
Library table:
ID - primary key
TITLE, etc - data fields

R_Library_Library table:
ParentLibraryId - foreign key to Library table
ChildLibraryId - another foreign key to Library table



The SQL to create this is
Code:
CREATE TABLE
LIBRARY
(
   ID int IDENTITY NOT NULL,
   TITLE varchar(max)  NOT NULL,
-- plus other fields
)
GO

CREATE TABLE
R_LIBRARY_LIBRARY
(
   PARENTLIBRARYID int  NOT NULL,
   CHILDLIBRARYID int  NOT NULL
)
GO

ALTER TABLE LIBRARY
ADD CONSTRAINT PK_LIBRARY
PRIMARY KEY
   CLUSTERED (ID ASC)
GO

ALTER TABLE R_LIBRARY_LIBRARY
ADD CONSTRAINT SYS_C0034348
FOREIGN KEY
   (CHILDLIBRARYID)
REFERENCES
   LIBRARY     (ID)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION
GO

ALTER TABLE R_LIBRARY_LIBRARY
ADD CONSTRAINT SYS_C0034347
FOREIGN KEY
   (PARENTLIBRARYID)
REFERENCES
   LIBRARY     (ID)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION
GO


And the Mapping:
Code:
   <class name="Library" table="library">
      <id name="Id" column="id" type="int">
         <generator class="identity" />
      </id>
      <property name="Title" column="title" />
      <!-- other fields ... -->
      
      <bag name="AssociatedDocuments" table="R_LIBRARY_LIBRARY">
         <key column="ParentLibraryId" />
         <many-to-many class="LibrarySearchSummary" column="ChildLibraryId" not-found="ignore" />
      </bag>
   </class>


I'm pretty stumped as to how to proceed. I could just get rid of the foreign key, which seems like the lazy way out. I could change the ON DELETE NO ACTION to something else, but that's not much better than no foreign key, and wasn't how it worked in Oracle. Or I could change the mappings, but I don't know how.

Any help please?


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.