Nhibernate:阻止它加入不需要的表

发布于 2024-09-04 18:39:51 字数 1112 浏览 2 评论 0 原文

我有两个与以下类相关的表(tbArea、tbPost)。

class Area
{
    int ID
    string Name
    ...
}

class Post
{
    int ID
    string Title
    Area Area
    ...
}

这两个类与 Fluent Nhibernate 映射。下面是帖子映射。

public class PostMapping : ClassMap<Post>
{
    public PostMapping()
    {
        Cache.NonStrictReadWrite();

        this.Table("tbPost");

        Id(x => x.ID)
            .Column("PostID")
            .GeneratedBy
            .Identity();

        References(x => x.Area)
            .ForeignKey("AreaID")
            .Column("AreaID");
        ...
    }
}

每当我对 Post 表“where AreaID = 1(any AreaId)”执行查询时,nhibernate 都会连接到区域表。

(Nhibernate 为查询生成的内容)

SELECT
    post fields
,   area fields (automatically added)
FROM tbPost p
LEFT JOIN tbArea a on 
        p.areaid = a.areaid
where
    p.areaid = 1

我尝试将 Area 设置为 LazyLoad、Fetch.Select、ReadOnly 以及引用上的任何其他设置,但它仍然会始终连接到 Area。

我正在尝试优化后端数据库查询,并且由于我不需要加载刚刚过滤的区域对象,所以我想在每次查询发布时消除对区域的不必要的连接。

我需要更改哪些配置或映射才能使区域仍然与对象中的帖子相关,但在筛选 AreaID 时不查询它?

I have two tables (tbArea, tbPost) that relate to the following classes.

class Area
{
    int ID
    string Name
    ...
}

class Post
{
    int ID
    string Title
    Area Area
    ...
}

These two classes map up with Fluent Nhibernate. Below is the post mapping.

public class PostMapping : ClassMap<Post>
{
    public PostMapping()
    {
        Cache.NonStrictReadWrite();

        this.Table("tbPost");

        Id(x => x.ID)
            .Column("PostID")
            .GeneratedBy
            .Identity();

        References(x => x.Area)
            .ForeignKey("AreaID")
            .Column("AreaID");
        ...
    }
}

Any time I perform a query on the Post table "where AreaID = 1(any AreaId)", nhibernate will join to the area table.

(What Nhibernate generates for a query)

SELECT
    post fields
,   area fields (automatically added)
FROM tbPost p
LEFT JOIN tbArea a on 
        p.areaid = a.areaid
where
    p.areaid = 1

I have tried setting Area to LazyLoad, to Fetch.Select, ReadOnly, and any other setting on the reference and still it will always join to Area.

I am trying to optimize the backend database queries, and since I don't need the area object loaded just filtered I would like to eliminate the unnecessary join to Area each time I Query post.

What configurations do I need to change or mappings to get area to still be related to post in my objects, but not query it when I filter on AreaID?

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

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

发布评论

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

评论(3

つ可否回来 2024-09-11 18:39:51

继续搜索后,我发现问题在于 Nhibernate Linq 库,而不是我的映射或查询。我想我之所以无法找到有关此问题的更多信息,是因为大多数使用 ORM 的人不会查看生成的查询。无论如何,这是确认我的问题的链接。

http://codeofrob. com/archive/2009/10/22/why-linq2nhibernate-isnt-ready-for-生产-use.aspx

我想出了两种解决方案。虽然使用 CreateCriteria 实际上创建了正确的查询,但它与我实现访问层的方式不兼容。我仍然需要根据传入的 Expressions> 语句执行 where 语句。我发现的两种可能的解决方案是:

  1. 使用此库从 lambda 创建条件声明。

    http://code.google.com/p/nhlambdaextensions/

    lambda 扩展的功能没有达到我的预期,但可能适用于其他有相同问题的人。

  2. 最后,使用 Linq 进行查询的最佳解决方案是下载并构建 Nhibernate 3 的开发版本。NH3 中的新 Linq 提供程序运行良好,查询看起来正如我所期望的那样。

    http://sourceforge.net/projects/nhibernate/develop

After continuing to search, I found that the problem with lies with the Nhibernate Linq library rather than my mappings or queries. I imagine the reason why I wasn't able to find more on this problem is because most people using an ORM don't look at the queries that are generated. Anyway, here is the link confirming my problem.

http://codeofrob.com/archive/2009/10/22/why-linq2nhibernate-isnt-ready-for-production-use.aspx

There are two solutions I came up with. While using CreateCriteria did in fact create the correct queries, it didn't work with how I am implementing my access layer. I still needed to perform where statements based on Expressions<Func<T,bool>> statements passed in. The two possible solutions I found are:

  1. Use this library for creating criteria out of lambda statements.

    http://code.google.com/p/nhlambdaextensions/

    The lambda extensions didn't function as well as I had hoped, but might work for others with the same problem.

  2. In the end the best solution to using Linq with my queries was to download and build the development version of Nhibernate 3. The new Linq provider in NH3 works perfectly and the queries looked just as I would expect.

    http://sourceforge.net/projects/nhibernate/develop

相守太难 2024-09-11 18:39:51

查询码是什么?这应该有效。

s.CreateCriteria<Post>()
    .Add(Restrictions.Eq("Area.ID", 1))
    .List<Post>();

使用多对一的 id 属性时,不需要创建别名。因为该值已经在 Post 表中。如果您在任何其他 Area 属性上尝试执行此操作,则需要 CreateAlias()。

What is the query code? This should work.

s.CreateCriteria<Post>()
    .Add(Restrictions.Eq("Area.ID", 1))
    .List<Post>();

You do not need to create an alias when using a many-to-one's id property. Since, the value is already in the Post table. If you attempted this on any other Area property, you would need to CreateAlias().

审判长 2024-09-11 18:39:51

不熟悉 NHibernate,但据我所知它与 LINQ 类似。

例如,我有 2 个表:

Customer
订单

1 客户有 0 多个订单。

通过 LINQ 更新订单将更新附加的客户。在我的例子中,它正在将重复的客户插入表中。

我必须在订单类中实现一个 Detach() 方法。

伪代码:

public void Detach()
{
   this._ReferenceToCustomer = emptyCustomerReference;
}

想法是将客户与订单分离。这就是你所追求的吗?

Not familiar with NHibernate, but from what I know it's similar to LINQ.

For example, I have 2 tables:

Customer
Order

1 customer has 0-many orders.

Updating an order through LINQ will update an attached customer. In my instance it was inserting a duplicate customer into the table.

I had to implement a Detach() method for in the order class.

pseudo code:

public void Detach()
{
   this._ReferenceToCustomer = emptyCustomerReference;
}

Idea is to detach customer from the order. Is this is what you are after?

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