仅从连接表中选择列,无需连接

发布于 2024-08-26 05:58:26 字数 1199 浏览 6 评论 0原文

给定这些表:

create table Orders (
   Id INT IDENTITY NOT NULL,
   primary key (Id)
)

create table Items (
   Id INT IDENTITY NOT NULL,
   primary key (Id)
)

create table OrdersItems (
   OrderId INT not null,
   ItemId INT not null,
   primary key (OrderId, ItemId)
)

是否可以使用 HQL/criteria API 构造一个查询,该查询会产生以下 SQL:

SELECT
    [OrderId], [ItemId]
FROM
    [OrdersItems]

我已经尝试了这两种方法:

var hqlResults = session
    .CreateQuery("select order.id, item.id from Order order inner join order.Items item")
    .List();

var criteriaResults = session
    .CreateCriteria<Order>()
    .CreateAlias("Items", "item", NHibernate.SqlCommand.JoinType.None)
    .SetProjection(Projections.Property("id"), Projections.Property("item.id"))
    .List();

但是这两种方法都坚持生成连接(或者失败,因为连接不是存在,在使用条件中),导致 SQL 例如:

select order.Id,
       item.Id
from   Orders order
       inner join OrdersItems ordersItems
         on order.Id = ordersItems.ArticleId
       inner join Items item
         on ordersItems.CategoryId = item.Id

有没有办法让 NHibernate 生成一个仅从连接表中选择列的查询,而不需要连接?

Given these tables:

create table Orders (
   Id INT IDENTITY NOT NULL,
   primary key (Id)
)

create table Items (
   Id INT IDENTITY NOT NULL,
   primary key (Id)
)

create table OrdersItems (
   OrderId INT not null,
   ItemId INT not null,
   primary key (OrderId, ItemId)
)

Is it possible to use HQL/criteria API to contruct a query that results in the following SQL:

SELECT
    [OrderId], [ItemId]
FROM
    [OrdersItems]

I've tried both of these approaches:

var hqlResults = session
    .CreateQuery("select order.id, item.id from Order order inner join order.Items item")
    .List();

var criteriaResults = session
    .CreateCriteria<Order>()
    .CreateAlias("Items", "item", NHibernate.SqlCommand.JoinType.None)
    .SetProjection(Projections.Property("id"), Projections.Property("item.id"))
    .List();

But both approaches insist on generating a join (or fail because the join isn't present, in using criteria), resulting in SQL such as:

select order.Id,
       item.Id
from   Orders order
       inner join OrdersItems ordersItems
         on order.Id = ordersItems.ArticleId
       inner join Items item
         on ordersItems.CategoryId = item.Id

Is there any way to have NHibernate generate a query that selects columns only from the join table, without requiring a join?

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

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

发布评论

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

评论(1

折戟 2024-09-02 05:58:26

我怀疑是否有办法使用 HQL 来做到这一点,因为 HQL 处理 NHibernate 实体,而 OrderItems 不是实体。在这种情况下,看起来您实际上并没有使用任何 ORM 功能,因此您可以简单地执行 SQL 查询 - 如果您愿意,可以通过 NHibernate。只需调用 ISession.CreateSQLQuery() 即可。

编辑

怀疑 NHibernate 坚持执行连接的原因是这样的:您已向它询问 Id 属性OrderItem 实体,因此必须确保 OrderItem 表中确实存在这些 ID 的行。 OrderItems 表中可能存在一行,其 ID 在 OrderItem 中不存在。当然,这样做是糟糕的数据库设计,而且不太可能,但是 NHibernate 不能确定情况并非如此,除非它查看表模式并看到适当的外键 - 但我怀疑它会做类似的事情那。

但这只是我的猜测。您可以在NHibernate 论坛上询问开发人员更明确的答案。

I doubt that there's a way to do it using HQL, because HQL deals witn NHibernate entities and OrderItems is not an entity. In this case it looks like you're not actually using any ORM features, so you can simply do a SQL query - via NHibernate if you wish. Just call ISession.CreateSQLQuery().

Edit:

I suspect that the reason NHibernate insists on doing the join is this: you've asked it for the Id properties of Order and Item entities, so it must ensure that there are actually rows in the Order and Item table for those IDs. It's possible that a row exists in the OrderItems table with IDs that don't exist in Order or Item. Sure, it would be bad database design to do that and it's unlikely, but NHibernate can't be sure that this is not the case unless it looks at the table schema and sees the appropriate foreign keys - but I doubt that it does things like that.

This is just my speculation, though. You could ask on the NHibernate forum for a more definitive answer from the developers.

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