Hello everyone,
I've found a problem in NHibernate when I try to make a query by criteria, and a property I query by is declared in the base class.
Suppose I have 2 classes, BaseClass and InheritedClass, and, as their names show, InheritedClass is a subclass of a base class:
Code:
public class BaseClass
{
private int _id;
private string _name;
public string Name
{
get {return _name;}
set {_name = value;}
}
public int Id
{
get {return _id;}
set {_id = value;}
}
}
public class InheritedClass: BaseClass
{
private int _value;
public int Value
{
get {return _value;}
set {_value = value;}
}
}
Instances of both classes persist in a database and a mapping file is as follows:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="NHibernate.Examples.QuickStart.BaseClass, NHibernate.Examples" table="Base">
<id name="Id" >
<generator class="native" />
</id>
<property name="Name" />
<joined-subclass name="NHibernate.Examples.QuickStart.InheritedClass, NHibernate.Examples" table="Inherited">
<key column="Id" />
<property name="Value" />
</joined-subclass>
</class>
</hibernate-mapping>
The problem arises when I try to load all objects of InheritedClass with a property Name (inherited from the base class) set to, say, "Test":
Code:
// This code snippet fails to execute
ICriteria criteria = session.CreateCriteria(typeof(InheritedClass));
criteria.Add(Expression.Expression.Eq("Name", "Test"));
IList list = criteria.List();
As a result, I have a NullReferenceException and a stack trace is:
Code:
at NHibernate.Dialect.Dialect.IsQuoted(String name) in C:\Documents and Settings\Dima\Рабочий стол\NHibernate\nhibernate-0.8.3.0\src\NHibernate\Dialect\Dialect.cs:line 676
at NHibernate.Dialect.MsSql2000Dialect.UnQuote(String quoted) in C:\Documents and Settings\Dima\Рабочий стол\NHibernate\nhibernate-0.8.3.0\src\NHibernate\Dialect\MsSql2000Dialect.cs:line 279
at NHibernate.Persister.NormalizedEntityPersister.Alias(String name, Int32 tableNumber) in C:\Documents and Settings\Dima\Рабочий стол\NHibernate\nhibernate-0.8.3.0\src\NHibernate\Persister\NormalizedEntityPersister.cs:line 1630
at NHibernate.Persister.NormalizedEntityPersister.ToColumns(String alias, String property) in C:\Documents and Settings\Dima\Рабочий стол\NHibernate\nhibernate-0.8.3.0\src\NHibernate\Persister\NormalizedEntityPersister.cs:line 1546
at NHibernate.Expression.AbstractCriterion.GetColumns(ISessionFactoryImplementor factory, Type persistentClass, String property, String alias, IDictionary aliasClasses) in C:\Documents and Settings\Dima\Рабочий стол\NHibernate\nhibernate-0.8.3.0\src\NHibernate\Expression\AbstractCriterion.cs:line 58
at NHibernate.Expression.AbstractCriterion.GetColumns(ISessionFactoryImplementor factory, Type persistentClass, String property) in C:\Documents and Settings\Dima\Рабочий стол\NHibernate\nhibernate-0.8.3.0\src\NHibernate\Expression\AbstractCriterion.cs:line 32
at NHibernate.Expression.SimpleExpression.ToSqlString(ISessionFactoryImplementor factory, Type persistentClass, String alias, IDictionary aliasClasses) in C:\Documents and Settings\Dima\Рабочий стол\NHibernate\nhibernate-0.8.3.0\src\NHibernate\Expression\SimpleExpression.cs:line 64
at NHibernate.Expression.Junction.ToSqlString(ISessionFactoryImplementor factory, Type persistentClass, String alias, IDictionary aliasClasses) in C:\Documents and Settings\Dima\Рабочий стол\NHibernate\nhibernate-0.8.3.0\src\NHibernate\Expression\Junction.cs:line 85
at NHibernate.Loader.CriteriaLoader..ctor(IOuterJoinLoadable persister, ISessionFactoryImplementor factory, CriteriaImpl criteria) in C:\Documents and Settings\Dima\Рабочий стол\NHibernate\nhibernate-0.8.3.0\src\NHibernate\Loader\CriteriaLoader.cs:line 65
at NHibernate.Impl.SessionImpl.Find(CriteriaImpl criteria) in C:\Documents and Settings\Dima\Рабочий стол\NHibernate\nhibernate-0.8.3.0\src\NHibernate\Impl\SessionImpl.cs:line 4840
at NHibernate.Impl.CriteriaImpl.List() in C:\Documents and Settings\Dima\Рабочий стол\NHibernate\nhibernate-0.8.3.0\src\NHibernate\Impl\CriteriaImpl.cs:line 243
at NHibernate.Examples.QuickStart.UserFixture.InheritanceTest() in c:\documents and settings\dima\рабочий стол\nhibernate\nhibernate-0.8.3.0\src\nhibernate.examples\quickstart\userfixture.cs:line 96
However, the query by property Value works just fine.
Does anyone have a workaround to this problem?