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.