Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upgrading to NHibernate 3.1: Bug with LINQ, Any and mapped component collections

Tags:

nhibernate

We are currently upgrading from NHibernate 2 to 3.1. The following scenario worked with the old LINQ provider but doesn't work with the new provider in NHibernate 3.1.

Here is the mapping for the scenario:

public class FooDbMap : ClassMap<Foo>
{
    public FooDbMap()
    {
        Id(x => x.Id);

        HasMany(x => x.Bars)
            .Component(part =>
                {
                    part.Map(y => y.Name);
                })
            .KeyColumn("FooId")
            .Table("FooBars");
    }
}

NHibernate correctly generates the schema from the above mapping:

create table Foo (
    Id UNIQUEIDENTIFIER not null
)

create table FooBars (
    FooId UNIQUEIDENTIFIER not null,
    Name TEXT not null,
    primary key (FooId, Name)
)

However, the following query generates an error:

Session.Query<Foo>()
    .Where(foo => foo.Bars.Any())
    .ToList();

The error is: System.Data.SqlClient.SqlException : Invalid column name 'Id'.

The SQL that NHibernate has generated is:

select foo0_.Id as Id20_
from Foo foo0_
where exists (select bar1_.Id from Bar bar1_
              where foo0_.Id = bar1_.FooId)

Almost right, but not quite - at the last minute NHibernate gets it wrong and decides that there should be an Id column on the Bar table.

This problem didn't used to occur with the old Linq-to-Nhibernate provider.

I can think of a few workarounds, but is this an NHibernate bug or feature?

like image 982
cbp Avatar asked Nov 27 '25 13:11

cbp


1 Answers

It's a bug. You can vote here: NH-2692

like image 125
cremor Avatar answered Nov 30 '25 06:11

cremor