Nhibernate ICreteria 表之间的关系
我有两个实体,即“客户”和“订单”,它们独立存在,并且在映射文件中没有定义关系。
现在我希望 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不确定您是否可以使用 ICriteria API 执行此操作,因为 ICriteria 查询是针对特定对象创建的,但您应该能够使用 HQL 执行此操作:
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:
更好的方法是将映射中的客户和订单关联起来。
在映射中需要做更多的工作,但使用客户和订单对象编写查询和代码的工作量会更少。
A better way would be relating the customer and the order in the mapping.
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.
如果您的客户有订单收集,那么您可以使用此:
if your customer have orders collection then you could use this: