Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 674751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:44:30+00:00 2026-05-14T00:44:30+00:00

Given these tables: create table Orders ( Id INT IDENTITY NOT NULL, primary key

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T00:44:30+00:00Added an answer on May 14, 2026 at 12:44 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 386k
  • Answers 386k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer As EXIT_FAILURE is a macro, it makes no difference which… May 14, 2026 at 11:47 pm
  • Editorial Team
    Editorial Team added an answer Using .NET you can target Windows Mobile which is soon… May 14, 2026 at 11:47 pm
  • Editorial Team
    Editorial Team added an answer If all the paths begin with the same initial list… May 14, 2026 at 11:47 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.