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 7035307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:17:20+00:00 2026-05-28T01:17:20+00:00

Is it possible to make a left join, but order by a column in

  • 0

Is it possible to make a left join, but order by a column in the right table using hql?

For example we have these classes in the domain:

public class SomeDocument
{
    public virtual int Id { get; set; }
    public virtual DocumentPart Part1 { get; set; }
    public virtual DocumentPart Part2 { get; set; }
}

public class DocumentPart
{
   public virtual int Id { get; set; }
   public virtual string Content { get; set; }
}

Part1 and Part2 properties are nullable.

SQL:

create table SomeDocument
  (
    Id number(*, 0) not null,
    DocumentPart1_ID NUMBER,
    DocumentPart2_ID NUMBER
  );

create table DocumentPart
  (
    Id number(*, 0) not null,
    Content nvarchar2(250) not null
  );

Mapping:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Testapp.domain" namespace="Testapp.domain.model">
  <class name='SomeDocument' table='SomeDocument' lazy="false">
    <many-to-one name='Part1' class="Testapp.domain.model.DocumentPart, Testapp.domain" lazy="false" column='DocumentPart1_ID' cascade="save-update" />
    <many-to-one name='Part1' class="Testapp.domain.model.DocumentPart, Testapp.domain" lazy="false" column='DocumentPart2_ID' cascade="save-update" />
  </class>
</hibernate-mapping>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Testapp.domain" namespace="Testapp.domain.model">
  <class name='DocumentPart' table='DocumentPart' lazy="false">
    <property name='Content' column='Content' not-null="true"/>
  </class>
</hibernate-mapping>

and now if I try to order SomeDocuments by Part1.Content:

UnitOfWork.CurrentSession
          .CreateQuery("from SomeDocument doc order by doc.Part1.Content")
          .List<SomeDocument>();

I will get only rows where DocumentPart1_ID is not null. NHibernate will generate SQL like this:

select * from 
SomeDocument doc, 
DocumentPart docPart,
where doc.DocumentPart1_ID = docPart.Id
order by docPart.Content

but what I want is something like this:

select * from 
SomeDocument doc
left join DocumentPart docPart on doc.DocumentPart1_ID = docPart.Id
order by docPart.Content

My database is Oracle Database 11g Enterprise Edition Release 11.1.0.7.0 – 64bit

  • 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-28T01:17:21+00:00Added an answer on May 28, 2026 at 1:17 am

    Explicitly specify to nhibernate that you want a left join in the query.

    select doc from SomeDocument doc left join doc.Part1 part order by part.Content
    

    If you leave out the select doc from the query, NHibernate will select all the entities, in this case it would be equivalent to writing select doc, part. This would create a result set of type object[] that represents a tuple of SomeDocument, DocumentPart.

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

Sidebar

Related Questions

Is it possible to make a GridView scroll from left to right instead of
Is it possible to make a site with ASP.NET MVC Framework using .NET 2.0?
Is it possible to make such buttons ( http://img225.imageshack.us/img225/6452/buttonslw9.jpg ) using CSS? It should
Is it possible to make a none table based layout that allows #bottom element
I have a table tbl_Info : InfoId Text PrivacyTypeId UserName TypeId IsInfo InfoItemId Example
Is it possible to perform a JOIN sub query against a Common Table Expression?
I need to simulate the left join effect without using the left join key.
Is it possible make some handler that will do something when user shutdown computer
Possible Duplicate: Make iPhone app paid version replace free version on install from app
is possible to make own help message or attach own event on help option

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.