Uploaded image for project: 'NHibernate [Moved to GitHub]'
  1. NH-2946

Superfluous join when querying by foreign key given Id with LINQ

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 3.2.0.GA
    • Fix Version/s: 3.3.0.CR1
    • Component/s: Linq Provider
    • Labels:
      None
    • Sprint:

      Description

      I have a simple one-way one to many relationship, say comment *-- author

      when I do

      var comments = session.Query<Comment>().Where(c=>c.Author.Id == authorId).ToList();

      The SQL that gets generated ends up doing superfulous join to the Person table (where Author lives):

      select *
      from Comments c0_

      inner join Person person1_

      on c0_.AuthorId = person1_.Id

      where person1_.Id = 2 /* @p0 */

      Why do the join, instead of just doing:

      select * from Comments c0 where c0.AuthorId = 2

      What makes it worse in my case is that in real scenario, what I show here as Person is a table that is mapped (via <join/> mapping) to two tables, so I end up doing not one but two joins here.

        Attachments

          Issue links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                kkozmic k
              • Votes:
                1 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:

                  Who's Looking?