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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:13:56+00:00 2026-05-22T21:13:56+00:00

This is for a M:N relationship, with the collection being mapped in NHibernate as

  • 0

This is for a M:N relationship, with the collection being mapped in NHibernate as a Set.

The criteria query we were using previously “worked” but it did not populate the Skills collection properly, in that only the first/looking-for skill was brought down, even if the employee had multiple skills.

I changed it to a LINQ query and it fixes the issue, properly fetching all the skills of that employee.

Dim sId = 1 ' Just to have one for example
Dim lstEmployees = Session.CreateCriteria(Of Employee)() _
                    .CreateAlias("Skills", "s", NHibernate.SqlCommand.JoinType.LeftOuterJoin) _
                    .Add(Expression.Or(Expression.Eq("PrimarySkillId", sId),
                                       Expression.Eq("s.Id", sId))) _
                    .SetResultTransformer(New DistinctRootEntityResultTransformer()) _
                    .List(Of Employee)()

' Returns correct employees but only their first skill in the Skills collection, even if they have more than one

Dim lstEmployees = (From e In Session.Query(Of Employee)()
                  Where e.PrimarySkillId =sId OrElse e.Skills.Any(Function(s) s.Id = sId)
                  Select e Distinct).Fetch(Function(e) e.Skills).ToList()

' Returns correct employees and their Skills collection contains all of their skills

Does anyone understand what’s different about the two seemingly-equivalent queries?

  • 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-22T21:13:56+00:00Added an answer on May 22, 2026 at 9:13 pm

    Off the bat, the first query is doing the distinct separation after retrieving all rows, while the second query actually does a select distinct .... What’s likely happening is that it hydrates the Employee model with just the one Skill retrieved

    To make the first query actually do a select distinct ... you will need to use projections.

    Something like

    Session.CreateCriteria(Of Employee)() _
           .CreateAlias("Skills", "s", NHibernate.SqlCommand.JoinType.LeftOuterJoin)
           .Add(Expression.Or(Expression.Eq("PrimarySkillId", sId),
                              Expression.Eq("s.Id", sId))) _
           .SetProjection(Projections.Distinct("Id")) _
           .SetFetchMode("s", FetchMode.Eager)
    

    may work. Alternatively you can try to just set the fetch mode with the DistinctRootEntityResultTransformer

    Session.CreateCriteria(Of Employee)() _
           .CreateAlias("Skills", "s", NHibernate.SqlCommand.JoinType.LeftOuterJoin)
           .Add(Expression.Or(Expression.Eq("PrimarySkillId", sId),
                              Expression.Eq("s.Id", sId))) _
           .SetResultTransformer(Transformers.DistinctRootEntityResultTransformer) _
           .SetFetchMode("s", FetchMode.Eager)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sample data: I can flatten out parent-child relationship using this query.... Any help... Thanks
Using NHibernate with ASP.NET 4. I have had no issues retrieving objects, but I've
Ok so I have this relationship in Rails: class Position < ActiveRecord::Base belongs_to :company
I have this relationship : class Organization < ActiveRecord::Base has_many :users end class User
I am trying to model this relationship following this link http://www.javaworld.com/javaworld/jw-01-2008/images/datamodel.gif Its the usual
I have a relationship like this class User include Mongoid::Document include Mongoid::Timestamps embeds_one :setting
I have a relationship like this class Foo { static hasMany = [bars: Bar,
I've got m2m relationship like this: #main table CREATE TABLE products_product ( id integer
I have a many-to-many relationship like this: A user has_many organizations through affiliations and
So I want to design a team/player relationship like this: every player belongs to

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.