Oh man, my copy/paste from the doc got fubared. Here it is, for real ;) (check out those chapters I mentioned though, they'll help you get more fine grained control)
Very Important Note: If the <key> column of a <one-to-many> association is declared NOT NULL, NHibernate may cause constraint violations when it creates or updates the association. To prevent this problem, you must use a bidirectional association with the many valued end (the set or bag) marked as inverse="true". See the discussion of bidirectional associations later in this chapter. On Thu, Apr 2, 2009 at 11:15 PM, Hudson Akridge <[email protected]>wrote: > Declare the AdminMap HasMany as Inverse. For more details, check out > chapter 6.4 and 6.8 in the NHibernate docs: > > From the Doc - May cause constraint violations when it creates or updates > the association. To prevent this problem, > use a bidirectional association discussion of bidirectional associations > later in this chapter. > If the <key> column of a <one-to-many> association is declared NOT NULL, > NHibernateyou mustwith the many valued end (the set or bag) marked as > inverse="true". > > Also, as a suggestion, inversing a bag collection is considered a bad > practice due to performance issues. I would recommend an indexed list, set, > or something else. For the purposes of your example if that's all you're > building, don't worry about it. But it's something I'd consider if your code > is going to ever see the light of day in a production environment. > > On Wed, Apr 1, 2009 at 5:06 PM, Jeremy Wadsack <[email protected]> > wrote: > > > > I'm seeing this in two cases, I'll give one here. I have looked > > through lots of NHibernate docs and messages and this seems like it > > *should* be possible, so I'm wondering if there's something missing in > > the Fluent-NHibernate mapping. > > > > Here are the mappings (irrelevant parts redacted): > > > > > > public AdminMap() { > > HasMany( x => x.AdminAccesses ) > > .AsBag() > > .KeyColumnNames.Add("admin_id") > > .Cascade.AllDeleteOrphan() > > .Not.LazyLoad(); > > } > > > > > > public AdminAccessMap() { > > Id( x => x.Id, "admin_access_id" ); > > References( x => x.Admin, "admin_id" ); > > References(x => x.Group, "group_id"); > > Map(typeof(AdminAccess). > > GetProperty("_function", > > BindingFlags.NonPublic | BindingFlags.Instance), > > "function_id"); > > > > } > > > > > > My test case merely creates an instance, saves it, loads and deletes: > > > > > > Admin admin = new .Admin {Name = username, Password = > > password}; > > admin.AdminAccesses.Add(new AdminAccess > > { Admin = admin, Function = Function.AdminSettings, > > Group = group}); > > Admins.Insert(admin); > > > > Admins.Delete( admin ); > > Assert.That(Admins.GetById(admin.Id), Is.Null); > > > > > > NHibernate throws a SQL exception because it's trying to NULL the key > > field: > > > > System.Data.SqlClient.SqlException: Cannot insert the value NULL into > > column 'admin_id', table 'AK_PROD_UN.dbo.t_admin_access'; column does > > not allow nulls. UPDATE fails. > > The statement has been terminated. > > > > > > exec sp_executesql N'UPDATE t_admin_access SET admin_id = null WHERE > > admin_id = @p0',N'@p0 int',@p0=31 > > > > > > I know that I *could* allow NULL on the admin_id foreign key but I > > can't see why I would want to do that. The access list means > > absolutely nothing (and therefore is basically orphaned) if it has no > > admin_id. > > > > Am I missing something? Shouldn't this work in a bidirectional > > mapping? > > > > -- > > Jeremy Wadsack > > > > > --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
