c# - NHibernate + Fluent NHibernate exception -
problem:
there searches can stored in db. each search has collection of filters. there roles. each role may have (nullable column) default search assigned it. also, each search visible 0 or many roles (many-to-many relationship).
when try access search filters, nh tries access filters.defaultsearchid, doesn't exist in filters table.
db:
create table [dbo].[searches] ( id int identity(1,1) primary key, description nvarchar(2000) not null ); create table [dbo].[filters] ( id int identity(1,1) primary key, description nvarchar(2000) not null, searchid int not null references searches(id) ); create table [dbo].[roles] ( id int identity(1,1) primary key, name nvarchar(255) not null, defaultsearchid int null references searches(id) ); create table [dbo].[searchesroles] ( searchid int not null references searches(id), roleid int not null references roles(id) );
entities:
public class search { public virtual int id { get; set; } public virtual string description { get; set; } public virtual icollection<filter> filters { get; set; } public virtual icollection<role> roles { get; set; } } public class filter { public virtual int id { get; set; } public virtual string description { get; set; } public virtual search search { get; set; } } public class role { public virtual int id { get; set; } public virtual string name { get; set; } public virtual search defaultsearch { get; set; } }
mappings:
public class searchmap : classmap<search>{ public searchmap() { table("searches"); id(x => x.id).generatedby.identity(); map(x => x.description); hasmany(x => x.filters).inverse().cascade.all().asbag(); hasmanytomany(x => x.roles).table("searchesroles").parentkeycolumn("searchid").childkeycolumn("roleid"); } } public class filtermap : classmap<filter> { public filtermap() { table("filters"); id(x => x.id).generatedby.identity(); map(x => x.description); references(x => x.search).column("searchid"); } } public class rolemap : classmap<role> { public rolemap() { table("roles"); id(x => x.id).generatedby.identity(); map(x => x.name); references(x => x.defaultsearch).column("defaultsearchid"); } }
code:
class program { static void main() { var sessionfactory = createsessionfactory(); using (var session = sessionfactory.opensession()) { var search = session.get<search>(1); foreach (var filter in search.filters) { console.writeline(filter); } } } static isessionfactory createsessionfactory(){ string connectionstring = @"server=.\sql2008; user id = sa; pwd=1; database = nhbug;"; return fluently.configure() .database(mssqlconfiguration.mssql2008.connectionstring(connectionstring)) .mappings(m=>m.fluentmappings.addfromassembly(assembly.getexecutingassembly())).buildsessionfactory(); } }
error:
when accessing search.filters property, nhibernate tries access filters.defaultsearchid db column not supposed there. column exists in roles table not in filters.
question:
is invalid configuration, fluent nhibernate or nhibernate bug?
i'm using sql server 2008 r2, nhibernate 2.1.2 , fluent nhibernate 1.1.0.685, although issue exists in nhibernate 3 beta 2 well.
thank you.
update: here actual sql generated
update2: cdmdotnet, same error, same sql, unfortunately.
update3: actual exception
update4: particular use case of general bug: entity references other entities 'many-to-many' , on other side of 'many-to-many' assoc. other entity references source entity (defaultquery in case). nh goes nuts when accessing any child collection (one-to-many) of source entity (filters in case).
update5: sample data
update6: xml issued fluent nhibernate
update hasmany mapping on searchmap include keycolumn():
hasmany(x => x.filters).keycolumn("searchid").inverse().cascade.all().asbag();
Comments
Post a Comment