NHibernate property mapping: columns and formula

9.3k views Asked by At

When i map columns from the inspected table, i do this:

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

How can I make property mapping initialize a UserType with data retrieved by the formula's sql query?

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

fails with an exception "wrong number of columns".

Thanks in advance!

2

There are 2 answers

3
Michael Gattuso On

MyUserNameType should be a class level mapping so that you can map the result of the SQL function to a class. See these two posts for some possible help:

  1. Class and SQL Function example: http://thoughtspam.spaces.live.com/blog/cns!253515AE06513617!478.entry

  2. NHibernate Mapping with formula mapping example: http://thoughtspam.spaces.live.com/blog/cns!253515AE06513617!477.entry

0
Eugene Tolmachev On

I'm the author of the articles referenced by Michael. I had no idea people where still interested and I'm not sure it's applicable with the latest NHibernate. Here's a fresh link though: http://thoughtspam.wordpress.com/2007/12/19/nhibernate-property-with-formula/

example, using 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

}

And finally the SQL function:

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

Obviously, you’ll want the function to do something meaningful, but the idea is you return the PK for the entity and implement ILifecycle.