I'm using FluentNHibernate. I am not using auto-mapping. I have a base class that is subclassed. When I query against the base class then it performs an extra query against the subclass. Here's the (contrived) example of what I'm doing:
public class Foo
{
int Id;
string SomeValue;
}
I create another class that represents an audit record of the first and I inherit it:
public class FooAudit : Foo
{
DateTime DateModified;
}
I create separate mappings for each that go to their own tables:
public class FooMap : ClassMap<Foo>
{
public FooAuditMap()
{
Table("Foo");
Id(x => x.Id).Column("FOO_ID");
Map(x => x.SomeValue).Column("SOME_VALUE");
}
}
public class FooAuditMap : ClassMap<FooAuditMap>
{
public FooAuditMap()
{
Table("FooAudit");
CompositeId()
.KeyProperty(x => x.DateModified, c => c.ColumnName("AUDIT_DATE"));
.KeyProperty(x => x.Id, c => c.ColumnName("FOO_ID"));
Map(x => x.SomeValue).Column("SOME_VALUE");
}
}
I perform a query against Foo:
public virtual IEnumerable<Foo> List()
{
using (var session = SessionFactory.OpenSession())
{
return session.CreateCriteria<Foo>().List<Foo>();
}
}
Which then hits the DB twice, once to execute that query against Foo and again to FooAudit.
Why is it making two queries? I generated the HBM files and there is absolutely nothing linking these classes.
EDIT: For completeness, this is what the bootstrap config looks like.
public static ISessionFactory CreateSessionFactory()
{
return Fluently
.Configure()
.Database
(
FluentNHibernate.Cfg
.Db.MsSqlConfiguration.MsSql2005
.ConnectionString(GetConnectionString())
)
.Mappings(m => m
.FluentMappings.AddFromAssemblyOf<Foo>()
.Conventions.Add(typeof(EnumConvention)))
.BuildSessionFactory();
}