|
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
|