I have this domain model:
Code:
public class Publication
{
public virtual int Id { get; set; }
public virtual IList<EquipmentAttr> EquipmentAttributes { get; set; }
public virtual IList<PublicationAttr> PublicationAttributes { set; get; }
}
public class EquipmentAttr : IndexationAttr
{
public EquipmentAttr()
{
}
public EquipmentAttr(string attrKey, string attrValue, string attrType) : base(attrKey, attrValue, attrType)
{
}
}
public class PublicationAttr : IndexationAttr
{
public PublicationAttr()
{
}
public PublicationAttr(string attrKey, string attrValue, string attrType) : base(attrKey, attrValue, attrType)
{
}
}
public abstract class IndexationAttr
{
public virtual int Id { get; set; }
public virtual int IndexationEntityId { get; set; }
public virtual string AttrKey { get; set; }
public virtual string AttrValue { get; set; }
public virtual string AttrType { get; set; }
public IndexationAttr()
{
}
public IndexationAttr( string attrKey, string attrValue, string attrType)
{
AttrKey = attrKey;
AttrValue = attrValue;
AttrType = attrType;
}
public override bool Equals(object obj)
{
if (this.Id == ((IndexationAttr)obj).Id &&
this.AttrKey == ((IndexationAttr)obj).AttrKey &&
this.AttrValue == ((IndexationAttr)obj).AttrValue &&
this.AttrType == ((IndexationAttr)obj).AttrType)
return true;
return false;
}
}
and this mappings:
Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property" default-lazy="true">
<class xmlns="urn:nhibernate-mapping-2.2" name="Buscador.Domain.com.clarin.entities.IndexationAttr, Buscador.Domain" table="INDEX_ATTR_TABLE" abstract="true">
<id name="Id" type="System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="ATTR_ID" />
<generator class="identity" />
</id>
<discriminator type="String">
<column name="ATTR_TYPE" />
</discriminator>
<property name="IndexationEntityId" type="System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="INDEX_TABLE_ID" />
</property>
<property name="AttrKey" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="ATTR_KEY" />
</property>
<property name="AttrValue" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="ATTR_VALUE" />
</property>
<property name="AttrType" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="ATTR_VALUE_TYPE" />
</property>
<subclass name="Buscador.Domain.com.clarin.entities.EquipmentAttr, Buscador.Domain" discriminator-value="EQUI" />
<subclass name="Buscador.Domain.com.clarin.entities.PublicationAttr, Buscador.Domain" discriminator-value="PUBLI" />
</class>
</hibernate-mapping>
Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property" auto-import="true" default-cascade="none" default-lazy="true">
<class xmlns="urn:nhibernate-mapping-2.2" mutable="true" name="Buscador.Domain.com.clarin.entities.Publication, Buscador.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="INDEX_TABLE">
<id name="Id" type="System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="INDEX_ID" />
<generator class="identity" />
</id>
<bag name="EquipmentAttributes">
<key column="INDEX_TABLE_ID" />
<one-to-many class="Buscador.Domain.com.clarin.entities.EquipmentAttr, Buscador.Domain" />
</bag>
<bag name="PublicationAttributes">
<key column="INDEX_TABLE_ID" />
<one-to-many class="Buscador.Domain.com.clarin.entities.PublicationAttr, Buscador.Domain" />
</bag>
</class>
</hibernate-mapping>
Ok, so far.
Then, in a test:
Code:
var publication = _session.Get<Publication>(57);
var equipAtt = publication.EquipmentAttributes;
When i inspect "equipAtt", it has ALL the IndexationAttributes. It seems like NHibernate ignore "discriminator" field.
Here is the generated sql:
Code:
SELECT publicatio0_.INDEX_ID as INDEX1_1_0_, publicatio0_.INDEX_WEIGHT as INDEX2_1_0_, publicatio0_.PUBLICATION_CONTACTS_QTY as PUBLICAT3_1_0_, publicatio0_.PUBLICATION_DATE as PUBLICAT4_1_0_, publicatio0_.PUBLICATION_DELETED as PUBLICAT5_1_0_, publicatio0_.PUBLICATION_HIGHLIGHT as PUBLICAT6_1_0_, publicatio0_.PUBLICATION_ID as PUBLICAT7_1_0_, publicatio0_.PUBLICATION_SUBTITLE as PUBLICAT8_1_0_, publicatio0_.PUBLICATION_TYPE as PUBLICAT9_1_0_, publicatio0_.PUBLICATION_URL as PUBLICA10_1_0_, publicatio0_.PUBLICATION_VISITOR_QTY as PUBLICA11_1_0_, publicatio0_.VEHICLE_LOC_PART as VEHICLE12_1_0_, publicatio0_.VEHICLE_LOC_PROV as VEHICLE13_1_0_, publicatio0_.VEHICLE_LOC_LOC as VEHICLE14_1_0_, publicatio0_.VEHICLE_COLOR as VEHICLE15_1_0_, publicatio0_.VEHICLE_FIRST_OWNER as VEHICLE16_1_0_, publicatio0_.VEHICLE_FUEL_TYPE as VEHICLE17_1_0_, publicatio0_.VEHICLE_KM as VEHICLE18_1_0_, publicatio0_.VEHICLE_MAKE as VEHICLE19_1_0_, publicatio0_.VEHICLE_MODEL as VEHICLE20_1_0_, publicatio0_.VEHICLE_VERSION as VEHICLE21_1_0_, publicatio0_.VEHICLE_PIC_QTY as VEHICLE22_1_0_, publicatio0_.VEHICLE_PRICE as VEHICLE23_1_0_, publicatio0_.VEHICLE_PRICE_CURRENCY as VEHICLE24_1_0_, publicatio0_.VEHICLE_SEGMENT as VEHICLE25_1_0_, publicatio0_.VEHICLE_STATUS as VEHICLE26_1_0_, publicatio0_.VEHICLE_TYPE as VEHICLE27_1_0_, publicatio0_.VEHICLE_YEAR as VEHICLE28_1_0_, publicatio0_.USER_UID as USER29_1_0_, publicatio0_.USER_TYPE as USER30_1_0_ FROM INDEX_TABLE publicatio0_ WHERE publicatio0_.INDEX_ID=@p0;@p0 = 57
SELECT equipmenta0_.INDEX_TABLE_ID as INDEX3_1_, equipmenta0_.ATTR_ID as ATTR1_1_, equipmenta0_.ATTR_ID as ATTR1_0_0_, equipmenta0_.INDEX_TABLE_ID as INDEX3_0_0_, equipmenta0_.ATTR_KEY as ATTR4_0_0_, equipmenta0_.ATTR_VALUE as ATTR5_0_0_, equipmenta0_.ATTR_VALUE_TYPE as ATTR6_0_0_ FROM INDEX_ATTR_TABLE equipmenta0_ WHERE equipmenta0_.INDEX_TABLE_ID=@p0;@p0 = 57
Obviously, i need that NHibernate only brings me the "equipment attribute" in the list.
Thank you very much.