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.  [ 5 posts ] 
Author Message
 Post subject: Foreign Key Constraint exception
PostPosted: Fri Feb 29, 2008 7:30 pm 
Beginner
Beginner

Joined: Wed Aug 01, 2007 4:28 pm
Posts: 23
I have two tables, one called SandlotProfile and one called SandlotProfileDEA. They are a one-to-many relationship, i.e. SandlotProfile has many DEAs.

I'm having an issue where when I save the SandlotProfile, it tries to save the objects in the DEA list, but does't provide the foreign key back to SandlotProfile. When i set that foreign key column as nullable, then nhibernate saves the objects fine, and THEN goes and does updates to fill in the foreign keys. This seems weird--why doesn't it do it in the initial INSERT and shouldn't I be able to require that foreign key to be non-null.

I feel like i've done this plenty of times using NH but i'm going nuts about why its failing now (and of course I have a tight deadline!)

Here's some sample mapping code.

<class name="SandlotProfile" table="SandlotProfile">
<!-- Primary key -->
<id column="SandlotProfileID" type="int" name="ID" access="field.camelcase-underscore">
<generator class="native">
</generator>
</id>

<!-- properities and Many-to-one relationships snipped -->
<!-- One-To-Many relationships -->
<bag name="SandlotProfileDEAList" lazy="true" cascade="all-delete-orphan" >
<key column="SandlotProfileID" />
<one-to-many class="SandlotProfileDEA" />
</bag>
</class>

<class name="SandlotProfileDEA" table="SandlotProfileDEA">
<!-- Primary key -->
<id column="SandlotProfileDEAID" type="long" name="ID" access="field.camelcase-underscore">
<generator class="native">
</generator>
</id>

<!-- Many-to-one relationships -->
<many-to-one column="SandlotProfileID" name="SandlotProfileObject" class="SandlotProfile" not-found="ignore" cascade="save-update" access="field.camelcase-underscore" />

<!-- Standard Properties/Fields -->
<property column="DEASchedule" type="System.String" name="DEASchedule" not-null="true" access="field.camelcase-underscore" />
</class>

and heres my DDL:

CREATE TABLE [dbo].[SandlotProfile](
[SandlotProfileID] [int] IDENTITY(1,1) NOT NULL,
[FirstName] [nvarchar](100) NOT NULL,
[LastName] [nvarchar](100) NOT NULL,
CONSTRAINT [PK_SandlotProfile] PRIMARY KEY CLUSTERED
(
[SandlotProfileID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]


CREATE TABLE [dbo].[SandlotProfileDEA](
[SandlotProfileDEAID] [bigint] IDENTITY(1,1) NOT NULL,
[SandlotProfileID] [int] NOT NULL,
[DEASchedule] [varchar](5) NOT NULL,
CONSTRAINT [PK_SandlotProfileDEA] PRIMARY KEY CLUSTERED
(
[SandlotProfileDEAID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
CONSTRAINT [IX_UniqueDEA] UNIQUE NONCLUSTERED
(
[DEASchedule] ASC,
[SandlotProfileID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[SandlotProfileDEA] WITH CHECK ADD CONSTRAINT [FK_SandlotProfileDEA_SandlotProfile] FOREIGN KEY([SandlotProfileID])
REFERENCES [dbo].[SandlotProfile] ([SandlotProfileID])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[SandlotProfileDEA] CHECK CONSTRAINT [FK_SandlotProfileDEA_SandlotProfile]

My classes are the usual stuff. For many-to-many and one-to-manys I'm using generics IList<T> and this has worked fine in the past. For instance, my SandlotProfile class has the following property to hold the above relationship.

protected IList<SandlotProfileDEA> _sandlotProfileDEAList = new List<SandlotProfileDEA>();
public virtual IList<SandlotProfileDEA> SandlotProfileDEAList
{
get { return _sandlotProfileDEAList; }
set { _sandlotProfileDEAList = value; }
}


I'm using Sql Server 2005, asp.net 2.0 built using vs2008, and Nhibernate 1.2.0.4000

ANY HELP is GREATLY appreciated.

THanks,
Craig


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 03, 2008 9:06 am 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
This behavior is by design. See docs here:
http://www.hibernate.org/hib_docs/nhibe ... hild-bidir

_________________
Karl Chu


Top
 Profile  
 
 Post subject: Thanks!
PostPosted: Mon Mar 03, 2008 10:40 am 
Beginner
Beginner

Joined: Wed Aug 01, 2007 4:28 pm
Posts: 23
Ah, thats what i ended up doing. We use a couple of ORMs here, so I think i forgot that NH had that behavior. It would be nice if it were automatic though.

Thanks,
Craig


Top
 Profile  
 
 Post subject: one other thing
PostPosted: Mon Mar 03, 2008 1:08 pm 
Beginner
Beginner

Joined: Wed Aug 01, 2007 4:28 pm
Posts: 23
Question:

If i don't have the inverse enabled, can i just do this:

parent.Children.Add(Child1);

without doing this:

Child1.Parent = parent;
parent.Children.Add(Child1);

Thanks,
Craig


Top
 Profile  
 
 Post subject: Re: one other thing
PostPosted: Mon Mar 03, 2008 1:30 pm 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
fregas wrote:
If i don't have the inverse enabled, can i just do this: ...

If you have a bidirectional relationship, one of two sides must be 'inverse="true"'. In the case of a one-to-many, the non-inverse side must be the "one" side (that means the inverse side must be the "many" side).

If you map the Parent.Children collection as inversed, your code snippet will work. Just remember that the association is controlled by Child.Parent: If you add a Child to the Children collection but do not set Child.Parent accordingly, the relationship will not be saved.

_________________
Karl Chu


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