diff --git a/src/main/java/org/springframework/data/querydsl/QSort.java b/src/main/java/org/springframework/data/querydsl/QSort.java index a7c905beb..2043d30f2 100644 --- a/src/main/java/org/springframework/data/querydsl/QSort.java +++ b/src/main/java/org/springframework/data/querydsl/QSort.java @@ -19,14 +19,15 @@ import java.io.Serializable; import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import java.util.Stack; import org.springframework.data.domain.Sort; import org.springframework.util.Assert; +import org.springframework.util.StringUtils; import com.mysema.query.types.Expression; import com.mysema.query.types.OrderSpecifier; import com.mysema.query.types.Path; +import com.mysema.query.types.PathMetadata; /** * Sort option for queries that wraps a querydsl {@link OrderSpecifier}. @@ -145,24 +146,24 @@ public class QSort extends Sort implements Serializable { return and(Arrays.asList(orderSpecifiers)); } + /** + * Recursively creates a dot-separated path for the property path. + * + * @param path must not be {@literal null}. + * @return + */ private static String preparePropertyPath(Path path) { - Stack stack = new Stack(); - Path pathElement = path; - while (pathElement.getMetadata() != null && pathElement.getMetadata().getParent() != null) { + PathMetadata metadata = path.getMetadata(); + Path parent = metadata.getParent(); - stack.push(pathElement.getMetadata().getElement().toString()); - pathElement = pathElement.getMetadata().getParent(); + if (parent == null) { + return ""; } - StringBuilder sb = new StringBuilder(); - while (!stack.isEmpty()) { - sb.append(stack.pop()); - if (!stack.isEmpty()) { - sb.append("."); - } - } - return sb.toString(); + String basPath = preparePropertyPath(parent); + String element = metadata.getElement().toString(); + + return StringUtils.hasText(basPath) ? basPath.concat(".").concat(element) : basPath.concat(element); } - } diff --git a/src/test/java/org/springframework/data/querydsl/QSortUnitTests.java b/src/test/java/org/springframework/data/querydsl/QSortUnitTests.java index 841283e69..86bf7e163 100644 --- a/src/test/java/org/springframework/data/querydsl/QSortUnitTests.java +++ b/src/test/java/org/springframework/data/querydsl/QSortUnitTests.java @@ -17,6 +17,9 @@ package org.springframework.data.querydsl; import static org.hamcrest.Matchers.*; import static org.junit.Assert.*; +import static org.springframework.data.querydsl.QQSortUnitTests_WrapperToWrapWrapperForUserWrapper.*; +import static org.springframework.data.querydsl.QQSortUnitTests_WrapperToWrapWrapperForUserWrapper_WrapperForUserWrapper.*; +import static org.springframework.data.querydsl.QQSortUnitTests_WrapperToWrapWrapperForUserWrapper_WrapperForUserWrapper_UserWrapper.*; import java.util.List; @@ -26,6 +29,7 @@ import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort.Direction; import org.springframework.data.domain.Sort.Order; +import com.mysema.query.annotations.QueryInit; import com.mysema.query.types.OrderSpecifier; /** @@ -171,7 +175,7 @@ public class QSortUnitTests { @Test public void shouldCreateSortForNestedPathCorrectly() { - QSort sort = new QSort(QUserWrapper.userWrapper.user.firstname.asc()); + QSort sort = new QSort(userWrapper.user.firstname.asc()); assertThat(sort, hasItems(new Order(Direction.ASC, "user.firstname"))); } @@ -182,7 +186,7 @@ public class QSortUnitTests { @Test public void shouldCreateSortForDeepNestedPathCorrectly() { - QSort sort = new QSort(QWrapperForUserWrapper.wrapperForUserWrapper.wrapper.user.firstname.asc()); + QSort sort = new QSort(wrapperForUserWrapper.wrapper.user.firstname.asc()); assertThat(sort, hasItems(new Order(Direction.ASC, "wrapper.user.firstname"))); } @@ -193,10 +197,27 @@ public class QSortUnitTests { @Test public void shouldCreateSortForReallyDeepNestedPathCorrectly() { - QSort sort = new QSort( - QWrapperToWrapWrapperForUserWrapper.wrapperToWrapWrapperForUserWrapper.wrapperForUserWrapper.wrapper.user.firstname - .asc()); + QSort sort = new QSort(wrapperToWrapWrapperForUserWrapper.wrapperForUserWrapper.wrapper.user.firstname.asc()); assertThat(sort, hasItems(new Order(Direction.ASC, "wrapperForUserWrapper.wrapper.user.firstname"))); } + + @com.mysema.query.annotations.QueryEntity + static class WrapperToWrapWrapperForUserWrapper { + + @QueryInit("wrapper.user")// + WrapperForUserWrapper wrapperForUserWrapper; + + @com.mysema.query.annotations.QueryEntity + static class WrapperForUserWrapper { + + UserWrapper wrapper; + + @com.mysema.query.annotations.QueryEntity + static class UserWrapper { + + User user; + } + } + } } diff --git a/src/test/java/org/springframework/data/querydsl/UserWrapper.java b/src/test/java/org/springframework/data/querydsl/UserWrapper.java deleted file mode 100644 index 2cb823f3c..000000000 --- a/src/test/java/org/springframework/data/querydsl/UserWrapper.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright 2015 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.data.querydsl; - -import com.mysema.query.annotations.QueryEntity; - -/** - * @author Christoph Strobl - */ -@QueryEntity -public class UserWrapper { - - User user; - -} diff --git a/src/test/java/org/springframework/data/querydsl/WrapperForUserWrapper.java b/src/test/java/org/springframework/data/querydsl/WrapperForUserWrapper.java deleted file mode 100644 index b9f33249b..000000000 --- a/src/test/java/org/springframework/data/querydsl/WrapperForUserWrapper.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright 2015 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.data.querydsl; - -import com.mysema.query.annotations.QueryEntity; - -/** - * @author Christoph Strobl - */ -@QueryEntity -public class WrapperForUserWrapper { - - UserWrapper wrapper; - -} diff --git a/src/test/java/org/springframework/data/querydsl/WrapperToWrapWrapperForUserWrapper.java b/src/test/java/org/springframework/data/querydsl/WrapperToWrapWrapperForUserWrapper.java deleted file mode 100644 index f089b01ba..000000000 --- a/src/test/java/org/springframework/data/querydsl/WrapperToWrapWrapperForUserWrapper.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright 2015 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.data.querydsl; - -import com.mysema.query.annotations.QueryEntity; -import com.mysema.query.annotations.QueryInit; - -/** - * @author Christoph Strobl - */ -@QueryEntity -public class WrapperToWrapWrapperForUserWrapper { - - @QueryInit("wrapper.user")// - WrapperForUserWrapper wrapperForUserWrapper; -}