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.  [ 3 posts ] 
Author Message
 Post subject: searching superclass using ICriteria
PostPosted: Tue Jun 23, 2009 5:18 am 
Newbie

Joined: Mon Jun 22, 2009 6:47 pm
Posts: 2
I have two classes, base class and dervied class:
Code:
class Class1
{
  ..
}

class Class2 : Class1
{
  ..
}


I am trying to perform search using Criteria API, what I am facing is that I am getting the result twice the expected number of records, I mean NHibernate build two entities for the same record; one for Class1 and the other for Class2, note that Class1 and Class2 share the same table, the thing is that in my application I am using Class2 instance when insert/update/Retrieve, but when searching I am using instance of type Class1 (this is a requirement), the code for searching is as follow:
Code:
            ISession session = sessionFactory.OpenSession();

            // Create search criteria
            var search = session.CreateCriteria(typeof (Class1));

            if(id != null)
            {
                search.Add(Expression.Eq("Id", id));
            }
            if(creationUserIsSpecified)
            {
                search.Add(Expression.Eq("CreationUser", creationUser));
            }
            if(creationDate !=null)
            {
                search.Add(Expression.Eq("CreationDate", creationDate));
            }
            if(modificationUserIsSpecified)
            {
                search.Add(Expression.Eq("ModificationUser", modificationUser));
            }
            if(modificationDate!=null)
            {
                search.Add(Expression.Eq("ModificationDate", modificationDate));
            }
            if(typeIsSpecified)
            {
                search.Add(Expression.Eq("Type", type));
            }
            if(fileNameIsSpecified)
            {
                search.Add(Expression.Eq("FileName", fileName));
            }
            if(descriptionIsSpecified)
            {
                search.Add(Expression.Eq("Description", description));
            }
            if(categoryIsSpecified)
            {
                search.CreateCriteria("Category").Add(Expression.Eq("Name", categoryName));
            }
            if(userIsSpecified)
            {
                search.CreateCriteria("User").Add(Expression.Eq("Email", userEmail));
            }
            if(viewStatusIsSpecified)
            {
                search.Add(Expression.Eq("ViewStatus", viewStatus));
            }
            if(deleted!=null)
            {
                search.Add(Expression.Eq("Deleted", deleted));
            }

            return search.List<Class1>();


any idea why I am getting this? should I add restriction to the criteria to build only Class1 entities? if yes, how? and what type of restriction should I use?

The O/R mappings are the same for both classes, but as I said above, Class2 is used when Add/Retrieve/Update while Class1 is used when searching.


Top
 Profile  
 
 Post subject: Re: searching superclass using ICriteria
PostPosted: Tue Jun 23, 2009 8:49 am 
Newbie

Joined: Mon Jun 22, 2009 6:47 pm
Posts: 2
It seems because I am mapping the classes to the same table but I am not using any of the mapping inheritance's three strategies. Here are the mappings (Document is the base class, and DocumentContent extend it):

Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   namespace="namespace"
                   assembly="assemblyName">
  <class name="Document" table="[document]" lazy="false">
    <id name="Id" column="[document_id]" type="Int64" >
      <generator class="native"/>
    </id>
    <property name="Type" column="[type]" type="String" length="50"/>
    <property name="Filename" column="[filename]" type="String" length="50"/>
    <property name="Description" column="[description]" type="String" length="50"/>
    <many-to-one name="Category" column="[category_id]" class="Category" cascade="refresh"/>
    <many-to-one name="User" column="[user_id]" class="User" cascade="refresh"/>
    <property name="ViewStatus" column="[view_status]" type="int" />
    <property name="Deleted" column="[deleted]" type="Boolean" />

    <property name="CreationUser" column="[creation_user]" type="String" length="50"/>
    <property name="CreationDate" column="[creation_date]" type="DateTime" />
    <property name="ModificationUser" column="[modification_user]" type="String" length="50"/>
    <property name="ModificationDate" column="[modification_date]" type="DateTime" />
  </class>
</hibernate-mapping>


Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   namespace="namespace"
                   assembly="assemblyName">
  <class name="DocumentContent" table="[document]" lazy="false">
      <id name="Id" column="[document_id]" type="Int64" >
      <generator class="native"/>
    </id>

    <property name="Content" column="[content]" type="BinaryBlob" />
    <property name="Type" column="[type]" type="String" length="50"/>
    <property name="Filename" column="[filename]" type="String" length="50"/>
    <property name="Description" column="[description]" type="String" length="50"/>
    <many-to-one name="Category" column="[category_id]" class="Category" cascade="refresh"/>
    <many-to-one name="User" column="[user_id]" class="User" cascade="refresh"/>
    <property name="ViewStatus" column="[view_status]" type="int" />
    <property name="Deleted" column="[deleted]" type="Boolean" />

    <property name="CreationUser" column="[creation_user]" type="String" length="50"/>
    <property name="CreationDate" column="[creation_date]" type="DateTime" />
    <property name="ModificationUser" column="[modification_user]" type="String" length="50"/>
    <property name="ModificationDate" column="[modification_date]" type="DateTime" />
   
  </class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject: Re: searching superclass using ICriteria
PostPosted: Wed Mar 03, 2010 8:30 am 
Newbie

Joined: Thu Apr 02, 2009 2:33 am
Posts: 2
It's been a while since you posted this, but I would be interested in how solved this in the end?
Or is it still an open issue?
Any hints?
Thanks Sabne


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