Skip to content

NH-2239 - Wrong OrderBy in generated SQL when using ICriteria, Eager fetching and order by clauses in collection mappings #1075

@nhibernate-bot

Description

@nhibernate-bot

Alessandro Giorgetti created an issue — 14th July 2010, 7:42:00:

Consider this scenario:
you have 2 classes called 'Item' and 'Tag', Item holds a collection of tags, the relation is mapped as inverse and we have an order-by clause applied to the collection mapping (the tags are ordered by name); we want to fetch a list of Item classes along with all its related Tags (to optimize things) and we want the result to be sorted by some field of the main Item class.

The problem is: performing the query using ICriteria and setting the eager fetch mode on the tags collection you don't get the results with the order you expect (the one you specified in the ICriteria query), instead the order specified in the collection mapping (a for example) takes over and the object are sorted first for Tag's Name and then for the sort you specified.

This behavior do not happen if you use HQL queries and the correct sorting is applied.

I've made an indept blog post on the subject: http://www.primordialcode.com/blog/post/nhibernate-eager-fetch-order-strange-behavior-icriteria

Attached to this entry you can find a solution to replicate the problem.


Alessandro Giorgetti added a comment — 20th July 2010, 1:45:43:

This is creating many more problems than I thought when I opened the issue the first time, can you please raise the priority a bit?

Moreover the sample project I attached early had an error in the mapping, I'll add a new one right now.

Thanks.
Giorgetti Alessandro


Alessandro Giorgetti added a comment — 20th July 2010, 1:47:07:

Solution to reproduce the issue.
Fixed collection mapping.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions