tags:

views:

481

answers:

2

Hello!

I'm currently trying to solve a problem. I have a class table inheritance aka table-er-subclass (one main table + several others with additional data). In my app both base object instances and extended objects can exist. Now I want to be able to sometimes fetch only those base objects and sometimes both types. A simple example (both classes are mapped with all of their properties)

public class Base 
{
  public in ID {get; set;}
  public string Something {get; set;}
}

public class Extended : Base 
{
  public bool NewProp{get; set;}
}

now running hql query "from Base" would fetch both Base and Extedned objects. Is there any way to restrict such behavior to fetch only Base objects?

A: 

That's the mapping for the above sample (if there's any error forgive me it must be a typo cause the sample runs) <?xml version="1.0" encoding="utf-16"?> <hibernate-mapping auto-import="true" default-lazy="false" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:nhibernate-mapping-2.2"> <class name="Base, Test" table="base"> <id name="ID" access="property" column="ID" type="Int64" unsaved-value="0"> <generator class="sequence"> <param name="sequence">base_id_seq</param> </generator> </id> <property name="Something" access="property" type="String"> <column name="somethin"/> <joined-subclass name="Extended, Test" table="extended" schema="extended"> <key column="id" /> <property name="NewProp" access="property" type="Boolean"> <column name="newProp"/> </property> </joined-subclass> </class> </hibernate-mapping>

Duh!, I don't know why the formatting is wrong
+2  A: 

with HQL you should be able to use the "class" special property:

from Base b where b.class=Base

another approach could be to use plain SQL where you have greater control of what you retrieve.

Anyway check the (N)Hibernate docs.

unfortunately I get message:NHibernate.QueryException: could not resolve property: class of:
it should definitely work, post your code.
:/ It's working, now. But eventually I came up with answer why it didn't. Base and Extended are in different assemblies. I registered Base but forgot about Extended - when base class doesn't have subclasses - it doesn't have "class" property.