3
votes

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?

2

2 Answers

2
votes

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

1
votes

Bug is still present in NHibernate 3.3.3.

As a workaround, you can use .Count() > 0:

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