NHibernate属性映射:列和公式 [英] NHibernate property mapping: columns and formula

查看:111
本文介绍了NHibernate属性映射:列和公式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我映射检查表中的列时,我会这样做:

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>

如何使属性映射使用由公式的sql查询检索的数据初始化UserType?

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".

提前谢谢!

推荐答案

我是Michael所引用文章的作者.我不知道人们仍然对它感兴趣,并且我不确定它是否适用于最新的NHibernate. 不过,这里有一个新的链接: http://thoughtspam.wordpress .com/2007/12/19/nhibernate-property-with-formula/

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/

示例,使用罗斯文(Northwind)...

example, using Northwind...

映射:

<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>

实体:

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.

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

这篇关于NHibernate属性映射:列和公式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆