本文介绍了流利的NHibernate和存储过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基本的Customer / Order / OrderItem / Product对象图。客户有许多订单,订单有很多订单项目,产品有很多订单项目。这些都是成功映射使用FNH。



我碰到一个配置存储过程的障碍&流利,NHibernate的。在Fluent-Hibernate FNH(版本1.0 RTM)中映射存储过程的本地方法并不多。有一个解决方案这里是关于向类映射中添加部分的部分,但AddPart调用已经从FNH的版本中拿出来。



存储过程很简单:

  CREATE PROCEDURE [dbo]。[OrderCountByCustomer] 
AS
BEGIN
SET NOCOUNT ON;

SELECT
c.name as [Customer.Name],
CAST(count(o.id)as NVARCHAR)as [Customer.OrderCount]
FROM customer c
LEFT OUTER JOIN [order] o
ON o.customer_id = c.id
GROUP BY c.name

END


$ b $ <$ p
$ b

中有一个CustomerOrderSummary.hbm.xml $ c><?xml version =1.0encoding =utf-8?> 
< sql-query name =OrderSummary>
< return class =CustomerOrderSummary>
< return-property column =Customer.Namename =CustomerName/>
< return-property column =Customer.OrderCountname =OrderCount/>
< / return>
EXEC [OrderCountByCustomer]
< / sql-query>
< / hibernate-mapping>

这里是CustomerOrderSummary类def:

<$命名空间NVAble.Orders.Core
{
public class CustomerOrderSummary
{
virtual public string CustomerName {get;组; }
虚拟公共字符串OrderCount {get;组; }
$ b $ public override string ToString()
{
return string.Format({0} {1},CustomerName,OrderCount);



code
$ b

然而,当试图启动NH会话我得到错误的命名查询 OrderSummary 没有其他细节。



这个简单的将 CustomerOrderSummary 类映射到过程,我不知道。该域对象显然不直接映射到数据库中的表,所以我不确定是否有一个正常的< class /> HBM映射将工作? / p>

在此先感谢!

解决方案

调查。我需要一个Domain Class的映射以及一个命名的查询hbm.xml文件。

在我的配置类中,我有

  config.Mappings(x => 
{
x.FluentMappings.AddFromAssemblyOf< CustomerMapping>().ExportTo(schemaPath);
x.HbmMappings.AddFromAssemblyOf< CustomerOrderSummary>();
});

唯一的缺点是我需要手动为存储过程创建xml映射,我不能在当前时间使用FNH

I have a basic Customer/Order/OrderItem/Product object graph. Customer has Many Orders, Order has Many Order Items, Product has many Order Items. These are successfully mapped using FNH.

I've hit a snag with configuring a stored procedure & fluent-nhibernate. There is not a native way to map stored procedures in fluent-hibernate FNH (version 1.0 RTM). There was a solution here about adding parts to class mappings but the AddPart call has been taken out of the release of FNH.

The stored procedure is simple:

CREATE PROCEDURE [dbo].[OrderCountByCustomer]
AS
BEGIN
    SET NOCOUNT ON;

    SELECT
        c.name as [Customer.Name],
        CAST(count(o.id) as NVARCHAR) as [Customer.OrderCount]
    FROM customer c
        LEFT OUTER JOIN [order] o
        ON o.customer_id = c.id
    GROUP BY c.name

END

There's a CustomerOrderSummary.hbm.xml in

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NVAble.Orders.Core" namespace="NVAble.Orders.Core">
    <sql-query name="OrderSummary">
        <return class="CustomerOrderSummary">
            <return-property column="Customer.Name" name="CustomerName" />
            <return-property column="Customer.OrderCount" name="OrderCount" />
        </return>
        EXEC [OrderCountByCustomer]
    </sql-query>
</hibernate-mapping>

Here is the CustomerOrderSummary class def:

namespace NVAble.Orders.Core
{
    public class CustomerOrderSummary
    {
        virtual public string CustomerName { get; set; }
        virtual public string OrderCount { get; set; }

        public override string ToString()
        {
            return string.Format("{0} {1}", CustomerName, OrderCount);
        }
    }
}

However, when try to start a NH session i get error in named query OrderSummary with no other details.

I'm probably missing something really simple that maps the CustomerOrderSummary class to the procedure, I don't know. That domain object obviously doesn't map directly to a table in the data base so I'm unsure if having a normal <class /> HBM mapping would work?

Thanks in advance!

解决方案

Ok, so after a bit more investigation. I needed a mapping for the Domain Class as well as a named query hbm.xml file.

In my configure class i have

config.Mappings(x =>
{
    x.FluentMappings.AddFromAssemblyOf<CustomerMapping>().ExportTo(schemaPath);
    x.HbmMappings.AddFromAssemblyOf<CustomerOrderSummary>();
});

Only downside is that I need to manually create the xml mapping for the stored procedure, I can't use FNH at the current time

这篇关于流利的NHibernate和存储过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 08:34