Valentin V
Valentin V

Reputation: 25799

NHibernate + Fluent NHibernate exception

Problem:

There are searches that can be stored in the DB. Each search has a collection of filters. Also there are roles. Each role may have (nullable column) a default search assigned to it. Also, each search is visible to zero or many roles (many-to-many relationship).

When I try to access the search filters, NH tries to access filters.DefaultSearchId, which 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 the search.Filters property, NHibernate tries to access Filters.DefaultSearchId db column which is not supposed to be there. This column exists in Roles table but not in filters.

QUESTION:

Is it invalid configuration, Fluent NHibernate or NHibernate bug?

I'm using SQL Server 2008 R2, NHibernate 2.1.2 and Fluent NHibernate 1.1.0.685, although this issue exists in NHibernate 3 beta 2 as well.

Thank you.

UPDATE: Here is the actual SQL generated

UPDATE2: CDMDOTNET, same error, same sql, unfortunately.

UPDATE3: Actual exception

UPDATE4: This is a particular use case of a general bug: Entity references other entities as 'many-to-many' and on the other side of 'many-to-many' assoc. the other entity references the source entity (DefaultQuery in my case). NH goes nuts when accessing any child collection (one-to-many) of a source entity (Filters in my case).

UPDATE5: Sample data

UPDATE6: XML issued by Fluent NHibernate

Upvotes: 3

Views: 1158

Answers (2)

Stefan Steinegger
Stefan Steinegger

Reputation: 64658

You didn't specify the column name for the Filters bag. It should be set to SearchId.

Upvotes: 0

Phill
Phill

Reputation: 18814

Update the HasMany mapping on the SearchMap to include the KeyColumn():

HasMany(x => x.Filters).KeyColumn("SearchId").Inverse().Cascade.All().AsBag();

Upvotes: 4

Related Questions