Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top