Mapping NHibernate Properties: Columns and Formulas

When I map the columns to the checked table, I do this:

<property name="InstanceName" type="MyNameUserType, MyApp.MyNamespace">
   <column name="Name"/>
   <column name="Name2"/>
</property>

How can I do a property mapping to initialize UserType with data retrieved by sql formula?

<property name="InstanceName" type="MyNameUserType, MyApp.MyNamespace" formula="(...)"/>

fails with the error "the wrong number of columns."

Thanks in advance!

+3
source share
2 answers

MyUserNameType must be a class level mapping so that you can map the result of the SQL function to the class. For some help see these two posts:

+1

, . , , , NHibernate. : http://thoughtspam.wordpress.com/2007/12/19/nhibernate-property-with-formula/

, Northwind...

Mapping:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
    <class name="PropertyFormulaExample.Shipper, PropertyFormulaExample" table="Shippers" lazy="false" >
        <id name="ShipperID" column="ShipperID" unsaved-value="0">
            <generator class="native" />
        </id>
        <property name="CompanyName" column="CompanyName" />
        <property name="Phone" column="Phone" />
    </class>
</hibernate-mapping>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
    <class name="PropertyFormulaExample.Order, PropertyFormulaExample" table="Orders" lazy="false">
        <id name="OrderID" column="OrderID" unsaved-value="0">
            <generator class="native" />
        </id>
        <property name="CustomerID" column="CustomerID" />
        <property name="ShipVia" type="PropertyFormulaExample.Shipper, PropertyFormulaExample" formula="dbo.GetShipper(shipvia)" />
    </class>
</hibernate-mapping>

Entities:

public class Order
{
    public int OrderID { get; set; }
    public string CustomerID { get; set; }
    public Shipper ShipVia { get; set; }
}

public class Shipper : ILifecycle
{
    public int ShipperID { get; set; }
    public string CompanyName { get; set; }
    public string Phone { get; set; }
    #region ILifecycle Members
    public LifecycleVeto OnDelete(NHibernate.ISession s)
    {
        throw new NotImplementedException();
    }
    public void OnLoad(NHibernate.ISession s, object id)
    {
    }
    public LifecycleVeto OnSave(NHibernate.ISession s)
    {
        throw new NotImplementedException();
    }
    public LifecycleVeto OnUpdate(NHibernate.ISession s)
    {
        throw new NotImplementedException();
    }
    #endregion

}

, , SQL:

CREATE FUNCTION dbo.GetShipper(@shipperId int)
RETURNS int
AS
BEGIN
RETURN @shipperId
END

, , - , , PK ILifecycle.

+1

Source: https://habr.com/ru/post/1724000/


All Articles