Here's the same query without using Lamdba Extensions:
ICriteria criteria = session.CreateCriteria(typeof(Issue))
.CreateCriteria("Calls", "contactAlias")
.Add(NHibernate.Criterion.Restrictions.Eq("ContactMethod", "Phone"))
I tried adding SetFetchMode("Calls", FetchMode.Join), but it didn't
change the query.
Here's the query NHibernate generates (without the named columns)
SELECT top 10 this_.*, callsali1_.*
FROM SERVICING.ISSUE this_
inner join SERVICING.CALL callsali1_
on this_.ISSUE_ID = callsali1_.ISSUE_ID
WHERE callsali1_.CONTACT_METHOD = 'Phone' /* @p0 */
I'm surprised that the SetFetchMode doesn't change the 'inner join' to
an 'outer join'.
When I loop through each issue and references the Calls collection,
NHibernate generates queries like this:
SELECT calls0.*
FROM CALL calls0
WHERE calls0.ISSUE_ID = '64377a98-cc0b-df11-9346-001e0bbed6b2' /*
@p0 */
I and found another thread on nhusers describing the same issue:
http://groups.google.com/group/nhusers/browse_thread/thread/f99437ac83335253
Is this a bug?
On Apr 8, 8:24 am, nadav s <[email protected]> wrote:
> i'm not using the new version
> create criteria adds a join to the Issue entity, but may be you need to
> tell it to explicitly fetch it from the results
> like SetFetchMode join for issue (may be its something like .Expand(Issue =>
> Issue.Calls) or Include
> (may be create criteria is not enough, but i'm just guessing).
>
> On Thu, Apr 8, 2010 at 12:03 AM, Chris J <[email protected]> wrote:
>
> > Does anyone have any thoughts on this?
>
> > To summarize my question more briefly -- if a criteria query returns
> > child records associated with the parent, why does it then query again
> > for the child records when I loop through the child collection? The
> > child collection is already populated from the criteria query.
>
> > Thanks.
>
> > On Apr 5, 4:14 pm, Chris J <[email protected]> wrote:
> > > I have a criteria issue that I cannot figure out. I have a table with
> > > a one-to-many relationship with a child table. Here is the mapping:
>
> > > <class name="Issue" table="ISSUE" lazy="true">
> > > <id name="Id" column="ISSUE_ID">
> > > <generator class="guid.comb" />
> > > </id>
> > > <property name="Status" column="STATUS" />
> > > <property name="Severity" column="SEVERITY" />
> > > <property name="Created" column="CREATED" />
> > > <bag name="Calls" lazy="true" cascade="all" inverse="true" >
> > > <key column="ISSUE_ID"></key>
> > > <one-to-many class="Calls"></one-to-many>
> > > </bag>
> > > </class>
>
> > > <class name="Calls" table="CALLS" lazy="true">
> > > <id name="Id" column="CALLS_ID">
> > > <generator class="guid.comb" />
> > > </id>
> > > <property name="ContactMethod" column="CONTACT_METHOD" />
> > > <property name="Created" column="CREATED" />
> > > <many-to-one name="Issue" column="ISSUE_ID" class="Issue" />
> > > </class>
>
> > > I have a criteria statement that constrains the result set based on
> > > the child. The resulting database query uses an inner join to query
> > > the child table, and the result set includes all of the data for the
> > > children. However, when I access the collection via the parent
> > > property in a foreach loop, NHibernate performs a new query to get
> > > each child. Also, a call to NHibernateUtil.IsInitialized returns
> > > false, even though I can see that the data was returned in the initial
> > > criteria query. Here's the code:
>
> > > ICriteria criteria = session.CreateCriteria<Issue>()
> > > .CreateCriteria((Issue t) => t.Calls)
> > > .Add<Calls>(c => c.ContactMethod ==
> > "Phone")
> > > .SetMaxResults(10);
>
> > > var list = criteria.List();
>
> > > foreach (Issue issue in list)
> > > {
> > > Console.WriteLine("Initialized? {0}. Issue {1}",
> > > NHibernateUtil.IsInitialized(issue.Calls),
> > > issue.IssueNumber );
>
> > > // Generates a new db query.
> > > foreach (var contact in issue.Calls)
> > > {
> > > Console.WriteLine( "{0}: {1}",
> > > contact.Id, contact.ContactMethod );
> > > }
>
> > > }
>
> > > Why doesn't NHibernate use the data that was returned from the
> > > criteria query?
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "nhusers" group.
> > To post to this group, send email to [email protected].
> > To unsubscribe from this group, send email to
> > [email protected]<nhusers%[email protected]>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/nhusers?hl=en.
>
>
--
You received this message because you are subscribed to the Google Groups
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/nhusers?hl=en.