I have a mapped view, which I would like to reference in another mapping.
Mapped View:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Model.TagBE, Model" table="vw_CurrentTag" lazy="true" schema="dbo">
<id name="Id" column="TAG_HISTORY_ID" type="Guid"
unsaved-value="00000000-0000-0000-0000-000000000000">
</id>
<property name="Tag" column="TAG"/>
<property name="TagStatus" column="TAG_STATUS"/>
<property name="Created" column="CREATE_DATE"/>
<many-to-one name="Defect" class="Model.DefectBE, Model" fetch="join"
not-found="ignore" cascade="none" column="DEFECT_ID" />
<many-to-one name="CreatedBy" class="Model.UserBE, Model" lazy="false"
fetch="join" cascade="none" column="CREATE_USER_ID" />
</class>
</hibernate-mapping>
I would like to use it in this mapping, to get a single TagBE:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="DefectBE, Model" lazy="true" table="DEFECT" schema="dbo">
<id name="Id" column="DEFECT_ID" type="Guid">
<generator class="assigned" />
</id>
<property name="Sn" column="SERIAL_NUMBER" />
<property name="Description" column="PART_DESCRIPTION" />
<many-to-one name="System" class="Model.SystemBE, Model" fetch="join"
cascade="none" column="SYSTEM_ID" not-found="ignore"/
</class>
</hibernate-mapping>
The above mappings have been trimmed down for the sake of an example (removed a bunch of properties and and man-to-one relationships not related to the problem at hand.
The many-to-one shown with the name “System” works as expected, and it is just another mapped table, not a view. My problem is that when I try to map the view “vw_CurrentTag”, I can only access the data when I specifically use ISession and ICriteria to get specificaly something of the TagBE class.
When I try to use the following in the DefectBE mapping, it just gives gives null for the TagBE:
<many-to-one name="Tag" class="Model.TagBE, Model" fetch="join" cascade="none"
column="DEFECT_ID" not-found="ignore"/>
I have also tried using one-to-one, but i always get null when the TagBE object part of a DefectBE object.
If it’s not possible I can always use a Bag with only 1 entry.
The view is just a query on a single table to return unique Tag column based on other columns.
the many-to-one “Tag” uses the column DEFECT_ID on the table DEFECT to match the primary key of the view TAG_HISTORY_ID which is never equal.
What you probably want is a one-to-one mapping on the Defect class which points to the DEFECT_ID on the view.
<one-to-one name="Tag" class="Model.TagBE, Model" fetch="join" property-ref="Defect" foreign-key="none"/>