diff --git a/modules/gui/src/com/haulmont/cuba/gui/data/impl/AbstractCollectionDatasource.java b/modules/gui/src/com/haulmont/cuba/gui/data/impl/AbstractCollectionDatasource.java index 10ccddf3d9..d9eff06140 100644 --- a/modules/gui/src/com/haulmont/cuba/gui/data/impl/AbstractCollectionDatasource.java +++ b/modules/gui/src/com/haulmont/cuba/gui/data/impl/AbstractCollectionDatasource.java @@ -482,6 +482,19 @@ public abstract class AbstractCollectionDatasource, K> } } + protected void setSortDirection(LoadContext.Query q) { + boolean asc = Sortable.Order.ASC.equals(sortInfos[0].getOrder()); + MetaPropertyPath propertyPath = sortInfos[0].getPropertyPath(); + // Sort on DB only if the property is not transient and it is not an entity. Sorting by entity in JPQL + // translates to order by entity's id in SQL, that makes no sense. + if (MetadataHelper.isPersistent(propertyPath) && !propertyPath.getMetaProperty().getRange().isClass()) { + QueryTransformer transformer = QueryTransformerFactory.createTransformer(q.getQueryString(), metaClass.getName()); + transformer.replaceOrderBy(propertyPath.toString(), !asc); + String jpqlQuery = transformer.getResult(); + q.setQueryString(jpqlQuery); + } + } + private class ComponentValueListener implements ValueListener { public void valueChanged(Object source, String property, Object prevValue, Object value) { refresh(); diff --git a/modules/gui/src/com/haulmont/cuba/gui/data/impl/CollectionDatasourceImpl.java b/modules/gui/src/com/haulmont/cuba/gui/data/impl/CollectionDatasourceImpl.java index 4bb73e5fd3..9f6aeca7bd 100644 --- a/modules/gui/src/com/haulmont/cuba/gui/data/impl/CollectionDatasourceImpl.java +++ b/modules/gui/src/com/haulmont/cuba/gui/data/impl/CollectionDatasourceImpl.java @@ -496,17 +496,6 @@ public class CollectionDatasourceImpl, K> detachListener((Instance) obj); } } - - protected void setSortDirection(LoadContext.Query q) { - boolean asc = Order.ASC.equals(sortInfos[0].getOrder()); - MetaPropertyPath propertyPath = sortInfos[0].getPropertyPath(); - if (MetadataHelper.isPersistent(propertyPath)) { - QueryTransformer transformer = QueryTransformerFactory.createTransformer(q.getQueryString(), metaClass.getName()); - transformer.replaceOrderBy(propertyPath.toString(), !asc); - String jpqlQuery = transformer.getResult(); - q.setQueryString(jpqlQuery); - } - } @SuppressWarnings("unchecked") public Map aggregate(AggregationInfo[] aggregationInfos, Collection itemIds) { diff --git a/modules/gui/src/com/haulmont/cuba/gui/data/impl/LazyCollectionDatasource.java b/modules/gui/src/com/haulmont/cuba/gui/data/impl/LazyCollectionDatasource.java index c8364f40e2..d9d35d43b5 100644 --- a/modules/gui/src/com/haulmont/cuba/gui/data/impl/LazyCollectionDatasource.java +++ b/modules/gui/src/com/haulmont/cuba/gui/data/impl/LazyCollectionDatasource.java @@ -359,15 +359,7 @@ public class LazyCollectionDatasource, K> LoadContext.Query q = createLoadContextQuery(ctx, params); if (q != null) { if (sortInfos != null) { - QueryTransformer transformer = QueryTransformerFactory.createTransformer(q.getQueryString(), metaClass.getName()); - - boolean asc = Order.ASC.equals(sortInfos[0].getOrder()); - MetaPropertyPath propertyPath = sortInfos[0].getPropertyPath(); - - transformer.replaceOrderBy(propertyPath.toString(), !asc); - String jpqlQuery = transformer.getResult(); - - q.setQueryString(jpqlQuery); + setSortDirection(q); } if (maxResults == 0 || data.size() < maxResults) {