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

The Archive Base Latest Questions

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

I am using NHibernate 3.2.1. The following query return session.Query<TmTranslation>() .Where(x => x.TranslationUnit.Document.Job ==

  • 0

I am using NHibernate 3.2.1. The following query

return session.Query<TmTranslation>()
              .Where(x => x.TranslationUnit.Document.Job == job)
              .OrderBy(x => x.Id)
              .ToList();

produces this SQL:

select   tmtranslation.id,
         tmtranslation.text,
         tmtranslation.fk_id_translation_unit

from     "TRANSLATION" tmtranslation
         inner join "TRANSLATION_UNIT" tmunit
           on tmtranslation.fk_id_translation_unit = tmunit.id
         inner join "TRANSLATION_UNIT" tmunit2
           on tmtranslation.fk_id_translation_unit = tmunit2.id
         inner join "DOCUMENT" tmdocument
           on tmunit2.fk_id_document = tmdocument.id
where    tmdocument.fk_id_job = 174
order by tmtranslation.id asc

My mapping:

public class TmTranslationMap : ClassMap<TmTranslation>
{
    public TmTranslationMap()
    {
        Table("\"TRANSLATION\"");
        LazyLoad();            
        Id(x => x.Id, "id").GeneratedBy.HiLo("hilo", "hilo_translation", "200");            
        Map(x => x.Text).Column("text");
        References<TmTranslationUnit>(x => x.TranslationUnit, "fk_id_translation_unit").Cascade.None();
        DynamicUpdate();
    }
}

public class TmTranslationUnitMap: ClassMap<TmTranslationUnit>
{
    public TmTranslationUnitMap()
    {
        Table("\"TRANSLATION_UNIT\"");
        LazyLoad();    
        Id(x => x.Id, "id").GeneratedBy.HiLo("hilo", "hilo_translation_unit", "200");
        HasMany(x => x.Translations).Inverse().KeyColumn("fk_id_translation_unit").Cascade.None();
        References<TmDocument>(x => x.Document, "fk_id_document").Not.Nullable().Cascade.None();
    }
}

public class TmDocumentMap : ClassMap<TmDocument>
{
    public TmDocumentMap()
    {
        Table("\"DOCUMENT\"");
        LazyLoad();          
        Id(x => x.Id, "id").GeneratedBy.HiLo("hilo", "hilo_document", "50");
        References<TmJob>(x => x.Job, "fk_id_job").Not.Nullable();
        HasMany(x => x.TranslationUnits).Inverse().KeyColumn("fk_id_document").Cascade.SaveUpdate();
    }
}

As you can see, one of the JOINs is useless and only makes the query run slower. Is there any way I can make the query not produce unnecessary JOINs using Linq?

Thank you.

  • 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-30T00:01:58+00:00Added an answer on May 30, 2026 at 12:01 am

    It seems that the LINQ to NHibernate provider has trouble navigating associations from the target to the source table and generates a separate join each time it encounters such an association: one for Translation -> TranslationUnit and one for TranslationUnit to Document.

    The trick is help the provider navigate in the other direction: Document -> TranlationUnit -> Translation like this:

    var items=(from doc in session.Query<Document>()
                from tu in doc.TranslationUnits
                    from translation in tu.Translations
               where doc.Job ==job                        
               orderby translation.Id
               select translation).ToList();
    

    The resulting SQL query is:

    SELECT     translatio2_.Id as Id1_, translatio2_.Text as Text1_, translatio2_.TmTranslationUnitId as TmTransl3_1_
    FROM         [Document] AS document0_ INNER JOIN
                      TmTranslationUnit AS translatio1_ ON document0_.Id = translatio1_.DocumentId INNER JOIN
                      TmTranslation AS translatio2_ ON translatio1_.Id = translatio2_.TmTranslationUnitId
    WHERE     (document0_.JobId = @p0)
    ORDER BY translatio2_.Id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am try to convert the following NHibernate query using dyanmic instantiation into an
I need to replicate the following working HQL query using criteria API. session.CreateQuery( select
I'm using a custom named query with NHibernate which I want to return a
How would I go about executing the following Hibernate query using the criteria API.
I am getting the following error when trying to update an object using nhibernate.
I am using Fluent NHibernate with an external 'hibernate.cfg.xml' file. Following is the configuration
I'm currently using StructureMap to inject instances of NHibernate ISessions using the following code:
I'm using Hibernate 3.3.1 and am following along in modelling this sample table structure
I am using Linq to NHibernate. I have a following test case : [TestMethod]
I have been following this nice walkthrough for creating a grid using MVC contrib.

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.