-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Query by Criteria - query by base class property problem
PostPosted: Thu Jun 02, 2005 4:18 am 
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?


Top
  
 
 Post subject:
PostPosted: Fri Jun 03, 2005 3:13 pm 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Please try 0.8.4, I believe this bug was fixed there.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.