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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:43:33+00:00 2026-05-23T15:43:33+00:00

I have two entities, Location and Industry, and a link-table between them. I’ve configured

  • 0

I have two entities, Location and Industry, and a link-table between them. I’ve configured a many-to-many relationship, in both directions, between the two entities.

In a search query, I’m trying to select Locations that are associated with a list of industries.

After days and days of trying to wrangle the criteria API, I’ve decided to drop down to HQL and abandon the criteria API. But even that isn’t going well for me – it seems, regardless of whether I hand-write this HQL query, or let the criteria API do it, I end up with the same result.

I managed to produce the right result in two ways – like this:

var q = Data.Query("select distinct loc from Location loc join loc.Industries ind where ind in (:ind)");

q.SetParameterList("ind", new Industry[] { Data.GetIndustry(4), Data.GetIndustry(5) });

And (better) like that:

var q = Data.Query("select distinct loc from Location loc join loc.Industries ind where ind.id in (:ind)");

q.SetParameterList("ind", new int[] { 4, 5 });

Unfortunately, both result in a sub-optimal query:

select distinct
  location0_.Id as Id16_,
  location0_.Name as Name16_,
  (etc.)
from Location location0_
  inner join LocationIndustry industries1_
    on location0_.Id=industries1_.LocationId
  inner join Industry industry2_
    on industries1_.IndustryId=industry2_.Id
where
  industry2_.Id in (? , ?)

Why the extra join?

Is NH not smart enough to know that the Industry.Id property, being the only Industry-property involved in the query, is stored in the LocationIndustry link-table, and there is no need for the extra join to the Industry table itself?

Or am I doing something wrong?

Ideally, the most intuitive thing for me would be to write:

from Location loc where loc.Industries in (:ind)

This does not work – it throws an error and says it does not know about the Industries property. I guess because Industries, being a “property” in programming terms, is actually a “relationship” in terms of DBMS.

What is the simplest and most efficient way to write this query in HQL?

Thanks!

  • 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-23T15:43:33+00:00Added an answer on May 23, 2026 at 3:43 pm

    I’m not sure you can avoid this extra join given the mapping strategy you have used.

    You could avoid it by using an intermediary class but this would mean you would need a class structure like this:

    public class Industry {
        //Other stuff
        public virtual List<LocationIndustry> LocationIndustries {get; set:;}
    }
    
    public class LocationIndustry {
        public virtual Location Location {get; set;}
        public virtual Industry Industry {get; set;}
    }
    
    public class Location {
        //normal stuff
        public virtual IList<LocationIndustry> LocationIndustries {get; set;}
    }
    

    Then you can query on the LocationIndustry class and avoid the join to Location.

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

Sidebar

Related Questions

I have two entities with many-to-many relationship defined on them. <set name=TreasuryCodes table=ServiceProviderAccountTreasuryCode lazy=true
I have two entities Foo and Bar with a Many to Many relationship between
I have two entities, Entity1 and Entity2, on a one to many relationship. On
I have two entities: Patient and Job. Patient has a to-many relationship to Job
I have two entities, a User and Role with a one-to-many relationship from user
i have two entities named Parent and Child , linked in a one-to-many relationship.
I have two entities, Job and Language , in a many-to-one relationship. The mapping
I have two entities: Location Post It's a 1.. * between Location and Post
I have two entities referenced one to many. When entity framework created the table
I have two entities: Group and ContactInfo. These are connected with a many-to-many relationship

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.