我的作者邁克爾引用的文章。我不知道哪些人仍然感興趣,我不確定它適用於最新的NHibernate。 這裏有一個新的鏈接,但:http://thoughtspam.wordpress.com/2007/12/19/nhibernate-property-with-formula/
例如,使用羅斯文...
映射:
<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。
感謝您的回答。 我試過你在post2中提供的方式,但它沒有給我提供任何東西。看起來像UserType只是沒有看到任何結果數據集的列:( – npeBeg
張貼的鏈接不工作對我來說 –
這篇文章已經超過一年了,我不控制互聯網。 ! –