Nhibernate ICreteria 表之间的关系

发布于 2024-08-09 04:27:31 字数 820 浏览 3 评论 0原文

我有两个实体,即“客户”和“订单”,它们独立存在,并且在映射文件中没有定义关系。

现在我希望 nhibernate 给出以下查询的结果:

select customer.id,customer.name from customer,order
where customer.id = order.id
and order.status = "Open"

我尝试使用投影,但是在检查输出 sql 时,我看到 Nhibernate 生成了一个子查询,这不是我的意图(并且性能较差?)

public IList<Customer> GetOpenOrders()
{
                    DetachedCriteria orders = DetachedCriteria.For<Order>("orders")  
                    .SetProjection(Projections.Property("orders.id"));

                    ICriteria cret = session.CreateCriteria(typeof(Customer)) 
                    .Add(Subqueries.PropertyIn("id", orders)) 
                    .Add(Expression.Eq("Status", "open"));

                    return cret.List<Customer>();
}

是否可以使用标准或者是否有更好的方法来完成此类查询?

I have two entities say Customer and Order which exist on their own and have no relationship defined in a mapping file.

Now I want nhibernate to give me the result of the following query:

select customer.id,customer.name from customer,order
where customer.id = order.id
and order.status = "Open"

I tried to use a projection but when examining the output sql I see Nhibernate generates a subquery which is not my intention (and less performant?)

public IList<Customer> GetOpenOrders()
{
                    DetachedCriteria orders = DetachedCriteria.For<Order>("orders")  
                    .SetProjection(Projections.Property("orders.id"));

                    ICriteria cret = session.CreateCriteria(typeof(Customer)) 
                    .Add(Subqueries.PropertyIn("id", orders)) 
                    .Add(Expression.Eq("Status", "open"));

                    return cret.List<Customer>();
}

Is it possible to do this with criterias or is there a better way to accomplish this sort of queries ?

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

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

发布评论

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

评论(3

笑梦风尘 2024-08-16 04:27:31

我不确定您是否可以使用 ICriteria API 执行此操作,因为 ICriteria 查询是针对特定对象创建的,但您应该能够使用 HQL 执行此操作:

select customer
from Customer customer, Orders order
where customer.id = order.id and order.status = 'Open'

I'm not sure if you can do it with the ICriteria API because ICriteria queries are created against a specific object but you should be able to do it with HQL:

select customer
from Customer customer, Orders order
where customer.id = order.id and order.status = 'Open'
抹茶夏天i‖ 2024-08-16 04:27:31

更好的方法是将映射中的客户和订单关联起来。

  1. 客户类具有订单集合作为属性。
  2. 订单类有一个客户作为属性。

在映射中需要做更多的工作,但使用客户和订单对象编写查询和代码的工作量会更少。

A better way would be relating the customer and the order in the mapping.

  1. The customer class has a collection of orders as property.
  2. The order class has a customer as property.
  3. Both

This is a little bit more work in the mapping, but it will be less work to write the queries and the code using the customer and order objects.

无人问我粥可暖 2024-08-16 04:27:31

如果您的客户有订单收集,那么您可以使用此:

ICriteria cret = session.CreateCriteria(typeof(Customer)) 
                    .CreateCriteria("orders") 
                          .Add(Expression.Eq("Status", "open"));

if your customer have orders collection then you could use this:

ICriteria cret = session.CreateCriteria(typeof(Customer)) 
                    .CreateCriteria("orders") 
                          .Add(Expression.Eq("Status", "open"));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文