Two classes mapped to the same table (one read-only) must be in the correct order?
Consider these two classes mapped to the same table. One of them is readonly via mutable = "false".
<class name="Funder" table="funder">
<id name="id">
<generator class="identity" />
</id>
<property name="funder_name" />
<property name="contact_name" />
<property name="addr_line_1" />
<property name="addr_line_2" />
<property name="addr_line_3" />
<property name="city" />
<many-to-one name="state" column="state_id" foreign-key="FK_funder_state_id" fetch="join" />
<property name="zip_code" length="10" />
<property name="phone_number" length="30" />
<property name="create_dt" update="false" not-null="true" />
<many-to-one name="create_by" column="create_by" not-null="true" update="false" foreign-key="FK_funder_create_by" fetch="join" />
<property name="last_update_dt" insert="false" />
<many-to-one name="last_update_by" insert="false" foreign-key="FK_funder_last_update_by" fetch="join" />
</class>
<class name="FunderSimple" table="funder" schema-action="none" mutable="false">
<id name="id">
<generator class="identity" />
</id>
<property name="funder_name" />
<property name="contact_name" />
<property name="phone_number" />
</class>
If I move the FunderSimple mapping before displaying the Funder, my schema does not generate correctly. If I leave it as above it works.
Is it by design? It seems that schema-action = "none" sticks to table_name and subsequent mappings in the same table will not generate schema.
I do it this way because I have another class called Contract which has a foreign key to the funder table. However, I don't need all the sponsor columns when referencing the contract object.
<many-to-one name="funder_simple" column="funder_id" foreign-key="FK_contract_funder_id" fetch="join" />
Funder does not inherit from FunderSimple.
Should I use another method to retrieve only a subset of columns from a foreign key table? Is many-to-one the only way to set up a foreign key?
using version 2.1.0.4000
source to share
In such situations, I use forecasts instead. I have never mapped two types to the same table (except in cases of inheritance).
So what am I doing in a situation like this:
create a FunderSimple class and import it so that it's known from NHibernate:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<import class="MyNamespace.FunderSimple" />
</hibernate-mapping>
Once you have done that, you can create a request for your Funder type with the ICriteria API, but you could specify that you want NHibernate to return FunderSimple instances. So NHibernate is smart enough to create a simplified SQL query that only retrieves the columns needed to populate the FunderSimple instances.
This is done as follows:
ICriteria crit = session.CreateCriteria (typeof(Funder));
// add some expressions ...
crit.Add ( ... );
// Now, set the projection, and specify that FunderSimple should be returned
crit.SetProjection (Projections.ProjectionList()
.Add (Projections.Property ("Id"), "Id")
.Add (Projections.Property ("funder_name"), "funder_name")
.Add (Projections.Property ("phone_number"), "phone_number"));
crit.SetResultTransformer (Transformers.AliasToBean (typeof(FunderSimple)));
crit.List <FunderSimple>();
source to share