| Basically I'm assembling a grid that is requested by some UI. The basic grid criteria is
session.createCriteria(Entity.class).createCriteria("tags").add(Restrictions.in("id", <some ids>)
the entity.getTags() has the @OrderBy annotation, causing the order by statement to be always appended to the query. To render a grid, we use this basic criteria for several queries. To prequery security and to know what the user is able to effectively see, an Projections.distinct(Projections.id()) is being applied to that query by default. So this approach does not work for SQL Server as the "ORDER BY items must appear in the select list if SELECT DISTINCT is specified." is then thrown. In other DB dialects, such as MySQL, this approach is working fine. So I currently have the following solutions. a) Redesign the subcriteria by using a sub query b) Change @OrderBy to e.g. @SortNatural As this is a query to just extract IDs, I think it is valid case to be able to explicitely turn off the automatic @OrderBy Generation to maintain cross DB compatibility, even though it is of course correct in probably 99% of all other cases. Another valid case (at least for me) where it might be useful to control this behaviour is when the same tag related property is supplied from the frontend as an order by parameter which basically then forces me to precheck my entity if that provided order field is not included as an @OrderBy property in the corresponding relationshop as adding the same order by twice also causes SQL server to fail. |