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].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.

Reply via email to