NHibernate property mapping: columns and formula

若如初见. 提交于 2019-12-23 18:34:09

问题


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!


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/1803532/nhibernate-property-mapping-columns-and-formula

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!