Note that there are some explanatory texts on larger screens.

plurals
  1. PONHibernate: Criteria - getting list of child id's
    primarykey
    data
    text
    <p>I have a couple of entities in a many to many relationship. Given the parent ID, how do I, using ICriteria, retrieve a list of children?</p> <p>Code so far:</p> <pre><code>var driverList = DriverGroup.CreateCriteria() .Add(Restrictions.IdEq(groupId)) .SetFetchMode("Drivers", FetchMode.Eager) .SetResultTransformer(Transformers.DistinctRootEntity) .UniqueResult&lt;DriverGroup&gt;().Drivers.Select(x =&gt; x.Id). ToArray(); var criteria = Driver.CreateCriteria() .Add(Restrictions.In("Id", driverList)); </code></pre> <p>However, the SQL generated is:</p> <pre><code>SELECT this_.groupId as groupId5_1_, this_.name as name5_1_, this_.type as type5_1_, drivers2_.groupId as groupId3_, ... fields snipped ... drivers3_.userId as ID3, ... fields snipped ... FROM dbo.Groups this_ left outer join dbo.Object_Rel drivers2_ on this_.groupId=drivers2_.groupId left outer join dbo.vwDriverSimple driverbase3_ on drivers2_.ID=driverbase3_.userID WHERE this_.type=0 AND this_.groupId = @p0; @p0 = 443 [Type: Int32 (0)] SELECT this_.userID as userID10_1_, ... fields snipped ... FROM dbo.drivers this_ WHERE this_.userID in ( @p0, ... lots of parameters ...); </code></pre> <p>How do I get it to generate the following?</p> <pre><code>SELECT this_.userID as userID10_1_, ... fields snipped ... FROM dbo.drivers this_ WHERE this_.userID in ( SELECT drivers3_.userId as ID3 FROM dbo.Groups this_ left outer join dbo.Object_Rel drivers2_ on this_.groupId=drivers2_.groupId left outer join dbo.vwDriverSimple driverbase3_ on drivers2_.ID=driverbase3_.userID WHERE this_.type=0 AND this_.groupId = @p0; @p0 = 443 [Type: Int32 (0)] ); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload