流畅的 NHibernate 和存储过程

发布于 2024-08-08 18:40:01 字数 1926 浏览 6 评论 0原文

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

我在配置存储过程和存储过程时遇到了障碍。流畅的休眠。在 fluid-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

这里有一个 CustomerOrderSummary.hbm.xml

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

这是 CustomerOrderSummary 类 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);
        }
    }
}

但是,当尝试启动 NH 会话时,我在命名查询 OrderSummary 中收到错误,没有其他详细信息。

我可能缺少一些非常简单的东西,将 CustomerOrderSummary 类映射到过程,我不知道。该域对象显然不会直接映射到数据库中的表,因此我不确定正常的 HBM 映射是否有效?

提前致谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寂寞陪衬 2024-08-15 18:40:01

好的,经过更多调查后。我需要域类的映射以及命名查询 hbm.xml 文件。

在我的配置类中,我

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

唯一的缺点是我需要手动创建存储过程的 xml 映射,目前我无法使用 FNH

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文