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

  • SEARCH
  • Home
  • 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 447225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T21:32:57+00:00 2026-05-12T21:32:57+00:00

I have a query I’ve written with NHibernate’s Criteria functionality and I want to

  • 0

I have a query I’ve written with NHibernate’s Criteria functionality and I want to optimize it. The query joins 4 tables. The query works, but the generated SQL is returning all the columns for the 4 tables as opposed to just the information I want to return. I’m using SetResultTransformer on the query which shapes the returned data to an Individual, but not until after the larger sql is returned from the server.

Here’s the NHibernate Criteria

        return session.CreateCriteria(typeof(Individual))
            .CreateAlias("ExternalIdentifiers", "ExternalIdentifier")
            .CreateAlias("ExternalIdentifier.ExternalIdentifierType", "ExternalIdentifierType")
            .CreateAlias("ExternalIdentifierType.DataSource", "Datasource")
            .Add(Restrictions.Eq("ExternalIdentifier.Text1", ExternalId))
            .Add(Restrictions.Eq("ExternalIdentifierType.Code", ExternalIdType))
            .Add(Restrictions.Eq("Datasource.Code", DataSourceCode))
            .SetResultTransformer(new NHibernate.Transform.RootEntityResultTransformer());

And the generated sql (from NHProfiler) is

SELECT (all columns from all joined tables)
FROM   INDIVIDUAL this_
       inner join EXTERNAL_ID externalid1_
         on this_.INDIVIDUAL_GUID = externalid1_.GENERIC_GUID
       inner join EXTERNAL_ID_TYPE externalid2_
         on externalid1_.EXTERNAL_ID_TYPE_GUID = externalid2_.EXTERNAL_ID_TYPE_GUID
       inner join SYSTEM_SRC datasource3_
         on externalid2_.SYSTEM_SRC_GUID = datasource3_.SYSTEM_SRC_GUID
WHERE  externalid1_.EXTERNAL_ID_TEXT_1 = 96800 /* @p0 */
       and externalid2_.EXTERNAL_ID_TYPE_CODE = 'PATIENT' /* @p1 */
       and datasource3_.SYSTEM_SRC_CODE = 'TOUCHPOINT' /* @p2 */

I only want the columns back from the Individual table. I could set a projection, but then I lose the Individual type.

I could also rewrite this with DetachedCriteria.

Are these my only options?

  • 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-12T21:32:57+00:00Added an answer on May 12, 2026 at 9:32 pm

    I had exactly the same question and asked one of NHibernate development team members directly, here is the answer I got:

    with the Criteria API, the only thing you could do is to use a projection list and include all of the properties of the root entity… then you would need to use the AliasToBeanResultTransformer. Far from an optimal solution obviously

    if you don’t mind rewriting the query with hql, then you can do it very easily. an hql query that says "from MyEntity e join e.Association" will select both the entity columns as well as the association’s columns (just like the problem you’re having with criteria). But an hql query that says "select e from MyEntity e join e.Association" will only select the columns of e.

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

Sidebar

Related Questions

I have query to show the table like this: but I want to PIVOT
I have query that LEFT joins information from two tables. With code with following
I have a question about JOINS. Does Sql JOINs reduce performance in a query?
I am using in C# MYsql .I have query that works if I run
I have a query in Access 2007. It's worked fine for months, but I'm
I have query regarding selected key in JavaScript. I am developing a sql query
I have three tables, 'A', 'B' and 'C'. I have query on 'A' and
I have query in link to SQL like this var password = (from userAccounts
I have query like this: $sql = SELECT * FROM global_info WHERE title LIKE
I want to have query like this in mysql: select a.x, a.y, a.foo, sum(a.bar)

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.