views:

330

answers:

1

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?

+1  A: 

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.

Evgeny
I am currently using SQL queries. I was hoping to move to the criteria API or at least HQL in order to simplify my code. I understand that I may be asking too much of NHibernate in this case, but if I'm only asking for the columns present in the join table, it *could* be smart enough to omit the join.
Kent Boogaart
Regarding your edit, I'm actually using the special "id" rather than "Id". This is supposed to encourage NH to avoid joins. If I select a FK from a table using "ForeignEntity.id" instead of "ForeignEntity.Id", it won't join to the foreign table and will instead just select against the foreign column in the parent table. Hence, in that case it isn't validating that the record actually exists either. Hmmm...
Kent Boogaart
Accepted as answer. From everything I tried and read I conclude that the answer is "no". I am using SQL queries instead. Oh well.
Kent Boogaart