-->
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.  [ 7 posts ] 
Author Message
 Post subject: .UniqueResult() returns multiple object due to inheritance
PostPosted: Wed Nov 02, 2005 10:12 am 
I have a query which should return only one row:
repository.Session.CreateCriteria(typeof(Employee)).
Add(NHibernate.Expression.Expression.Eq("ID", NBNo)).UniqueResult();

ID is not the primary key on the table but is unique. I am expecting to return a employee class, however I get three other empty objects back + 1 the one I requested.
all the other objects inherits from employee which I believe causes the problem.

How can i get around this ?


Top
  
 
 Post subject:
PostPosted: Thu Nov 03, 2005 10:55 am 
Contributor
Contributor

Joined: Thu May 12, 2005 9:45 am
Posts: 593
Location: nhibernate.org
How do you get three other empty objects back + 1 the one I requested?
UniqueResult() returns an object which should be the one you requested...

Are these other objects matching the criteria (ID=NBNo)?

_________________
Pierre Henri Kuaté.
Get NHibernate in Action Now!


Top
 Profile  
 
 Post subject: .UniqueResult() returns multiple object due to inheritance
PostPosted: Fri Nov 04, 2005 2:32 am 
yep they match the critieria. however it does not match the exact type. it matched the baseclass of the 3 objects type on the subclass's type


Top
  
 
 Post subject:
PostPosted: Thu Nov 10, 2005 2:29 pm 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
I'm still wondering how you get 4 objects out of a method that returns only one. I'm thinking the answer to the question lay somewhere in that problem.


Top
 Profile  
 
 Post subject: Re: .UniqueResult() returns multiple object due to inheritan
PostPosted: Thu Nov 10, 2005 3:30 pm 
Contributor
Contributor

Joined: Thu May 12, 2005 8:45 am
Posts: 226
jaded wrote:
ID is not the primary key on the table but is unique.

How is this uniqueness defined? If you are getting four records with the same ID, it must not be unique as NHibernate defines it. I'm not sure about how .UniqueResult() behaves, but it sounds to me like uniqueness is also related to type and/or primary key. What kind of inheritance model are you using?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 14, 2005 12:50 pm 
This is the mapping that i am using. Note that both planner and employee maps to the exact same table.


by using this function:
Code:
public T GetObjectByUniqueKey<T>(string field, string value)
   {
      
      return  (T)Session.CreateCriteria(typeof(T)).
            Add(NHibernate.Expression.Expression.Eq(field, value)).UniqueResult();
   }


and passing in e.g. 'planner' as the type i get null back because multiple records was returned??

to 'fix this problem i do the following:

Code:
static public T LookUpByEmployeeNBNo<T>(object NBNo)
      {
         //HACK: problem with nhibernate returning all subclasses on list
         
         if (NBNo == null) return default(T);
         IList employees = repository.Session.CreateCriteria(typeof(T)).
            Add(NHibernate.Expression.Expression.Eq("NBNo", NBNo)).List();
            if (employees.Count > 0)
            {
                foreach (object obj in employees)
                    if (obj.GetType() == typeof(T)) return (T)obj;
            }
         
         return default(T);

         
      }



looking at the Ilist result i look for an exact match of the type 'Planner' and then return that object

Key point is that planner inherits from employee however a employee is not a planner therefore only a planner should be returned!!

please help if any got an answer thanks!





Code:
<class name="Planner" table="Employee">
      <id name="ID" column="ID">
         <generator class="assigned" />
      </id>
      <property column="status" name="_status"></property>
      <many-to-one name="Branch" column="BranchID" class="Branch" />
      <property name="SurName" column="SurName" type="String" />
      <property name="NBNo" column="NBNo" type="String"/>
      <set name="Roles" cascade="all" >
         <key column="EmpId"/>
         <one-to-many class="EmployeeRole"/>
      </set>

   </class>


<!-- Employee -->
   <class name="Employee" table="Employee">
      <id name="ID" column="ID">
         <generator class="assigned" />
      </id>
      <property column="status" name="_status"></property>
      <many-to-one name="Branch" column="BranchID" class="Branch" />
      <property name="SurName" column="SurName" type="String" />
      <property name="NBNo" column="NBNo" type="String"/>
      <set name="Roles" cascade="all" >
         <key column="EmpId"/>
         <one-to-many class="EmployeeRole"/>
      </set>
      
   </class>


Top
  
 
 Post subject:
PostPosted: Mon Nov 14, 2005 12:58 pm 
Contributor
Contributor

Joined: Thu May 12, 2005 8:45 am
Posts: 226
If you are using one table, I suspect you need a discriminator column. http://nhibernate.sourceforge.net/nh-docs/html/mapping.html#mapping-declaration-discriminator


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.