This is by design in NHibernate. You either need to map the other side of the collection or allow your id to be nullable. I can't remember the exact reason this is, but it's something to do with not knowing the id to insert into that column at the time of save, so it as to do an update after it's created the other entity. Collections one-to-many<http://www.nhforge.org/doc/nh/en/index.html#collections-onetomany> See the Very Important Note at the end.
On Wed, Mar 25, 2009 at 3:21 PM, Greg Cook <[email protected]> wrote: > Hi, > I'm having trouble mapping a unidirectional collection relationship as > follows: > > > Subscription.........0...* -> SubscriptionDefinition > > I'm getting the following error: > > > NHibernate: INSERT INTO dbo.[SubscriptionDefinition] (LastModifiedDate, > Version, CreateBy, CreateDate, LastModifiedBy) > > VALUES (@p0, @p1, @p2, @p3, @p4); select SCOPE_IDENTITY(); @p0 = '2009/03/25 > 9:15:29 AM', @p1 = '1', @p2 = 'gcook', > @p3 = '2009/03/25 9:15:29 AM', @p4 = 'gcook' > > System.Data.SqlClient.SqlException: Cannot insert the value NULL into column > 'SubscriptionId', table 'xxx.dbo.SubscriptionDefinition'; column does not > allow nulls. INSERT fails. > The statement has been terminated. > > > I've noticed in the sql above that the foreign key column 'SubscriptionId' > is not part of the insert at all.... > Any help is greatly Appreciated. > > Class Maps > public SubscriptionMap() > { > Id(x => x.Id, "SubscriptionId"); > Map(x => x.Name, "Name").WithLengthOf(50).Unique().Not.Nullable(); > Map(x => x.ContactEmailAddress, "ContactEmailAddress") > .WithLengthOf(50).Not.Nullable(); > HasMany(x => x.SubscriptionDefinitions) > > .KeyColumnNames.Add("SubscriptionId").AsBag().Cascade.AllDeleteOrphan(); > } > > public SubscriptionDefinitionMap() > { > Id(x => x.Id, "SubscriptionDefinitionId"); > > JoinedSubClass<MessagingSubscriptionDefinition>("SubscriptionDefinitionId", > part => { > part.Map(m => > m.QueueName).Unique().WithLengthOf(50).Not.Nullable(); > part.Map(m => m.MessageLifeSpanInSeconds); > part.Map(m => m.UseBinaryFormatter); > }); > } > > > Domain Objects > public class Subscription : DomainObject, IEquatable<Subscription> > { > private string name; > private string contactEmailAddress; > private IList<SubscriptionDefinition> subscriptionDefinitions = new > List<SubscriptionDefinition>(0); > ..... > // properties get/set > > // Equals, GetHashCode()... > } > > public abstract class SubscriptionDefinition : DomainObject > { > } > > > public class MessagingSubscriptionDefinition : SubscriptionDefinition, > IEquatable<MessagingSubscriptionDefinition> > { > private int messageLifeSpanInSeconds; > private string queueName; > private bool useBinaryFormatter; > > public virtual string QueueName > { > get { return queueName; } > set { queueName = value; } > } > .... > // the rest of the obj... > } > > MAPPING TESTS > > [Test] > public void SubscriptionMapping() > { > new PersistenceSpecification<Subscription>(session) > .CheckProperty(x => x.Name, "Apple") > .CheckProperty(x => x.ContactEmailAddress, " > myemailaddresss.com") > .VerifyTheMappings(); > } > > [Test] > public void SubscriptionWithSubscriptionDefinitions() > { > List<SubscriptionDefinition> subscriptions = > new List<SubscriptionDefinition> > { > ObjectMother.CreateMessagingSubscriptionDefinition("foo"), > ObjectMother.CreateMessagingSubscriptionDefinition("bar"), > ObjectMother.CreateMessagingSubscriptionDefinition("another > 1") > }; > new PersistenceSpecification<Subscription>(session) > .CheckProperty(x => x.Name, "Apple") > .CheckProperty(x => x.ContactEmailAddress, "myemailaddresss.com > ") > .CheckList(x => x.SubscriptionDefinitions, subscriptions) > .VerifyTheMappings(); > } > > > TABLE DDL > CREATE TABLE [dbo].[Subscription]( > [SubscriptionId] [int] IDENTITY(1,1) NOT NULL, > [Name] [varchar](50) NOT NULL, > [ContactEmailAddress] [varchar](50) NOT NULL, > [Version] [int] NOT NULL, > [CreateDate] [datetime] NOT NULL > CONSTRAINT [SubscriptionCreateDateDefault] DEFAULT (getdate()), > [LastModifiedDate] [datetime] NOT NULL > CONSTRAINT [SubscriptionLastModifiedByDateDefault] DEFAULT (getdate()), > [CreateBy] [varchar](30) NOT NULL, > [LastModifiedBy] [varchar](30) NOT NULL > CONSTRAINT [PK_SubScription] PRIMARY KEY NONCLUSTERED > ( > [SubscriptionId] ASC > ) > ) > GO > PRINT 'table : SubscriptionDefinition' > GO > CREATE TABLE [dbo].[SubscriptionDefinition]( > [SubscriptionDefinitionId] [int] IDENTITY(1,1) NOT NULL, > [SubscriptionId] [int] NOT NULL, > [Version] [int] NOT NULL, > [CreateDate] [datetime] NOT NULL > CONSTRAINT [SubscriptionDefinitionCreateDateDefault] DEFAULT > (getdate()), > [LastModifiedDate] [datetime] NOT NULL > CONSTRAINT [SubscriptionDefinitionLastModifiedByDateDefault] DEFAULT > (getdate()), > [CreateBy] [varchar](30) NOT NULL, > [LastModifiedBy] [varchar](30) NOT NULL > CONSTRAINT [PK_SubScriptionDefinition] PRIMARY KEY NONCLUSTERED > ( > [SubscriptionDefinitionId] ASC > ) > ) > GO > ALTER TABLE [dbo].[SubscriptionDefinition] WITH CHECK > ADD CONSTRAINT [FK_SubscriptionDefinitionToSubscription] > FOREIGN KEY([SubscriptionId]) > REFERENCES [dbo].[Subscription] ([SubscriptionId]) > GO > > PRINT 'table : MessagingSubscriptionDefinition' > GO > CREATE TABLE [dbo].[MessagingSubscriptionDefinition]( > [SubscriptionDefinitionId] [int] NOT NULL, > [QueueName] [varchar](50) NOT NULL, > [MessageLifeSpanInSeconds] [int] NOT NULL, > [UseBinaryFormatter] [bit] NOT NULL > CONSTRAINT [PK_MessagingSubScriptionDefinition] PRIMARY KEY NONCLUSTERED > ( > [SubscriptionDefinitionId] ASC > ) > ) > GO > ALTER TABLE [dbo].[MessagingSubscriptionDefinition] WITH CHECK > ADD CONSTRAINT > [FK_MessasingSubscriptionDefinitionToSubscriptionDefinition] > FOREIGN KEY([SubscriptionDefinitionId]) > REFERENCES [dbo].[SubscriptionDefinition] ([SubscriptionDefinitionId]) > GO > > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Fluent NHibernate" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/fluent-nhibernate?hl=en -~----------~----~----~----~------~----~------~--~---
