Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort can not use the metamodel classes generated by QueryDSL [DATAMONGO-1085] #2005

Closed
spring-projects-issues opened this issue Nov 4, 2014 · 12 comments

Comments

@spring-projects-issues
Copy link

@spring-projects-issues spring-projects-issues commented Nov 4, 2014

Jordi Llach Fernandez opened DATAMONGO-1085 and commented

When working with QueryDSL I would like to be able to sort queries in a type-safe way, and not just when building queries.

To be able to sort by the metamodel classes generated when using QueryDSL the method getKeyForPath of class org.springframework.data.mongodb.repository.support.SpringDataMongodbSerializer must be improved in order to allow dealing with metadata names like : customer.lastName, customer.metadata.creationDate and so on (where metadata is an embedded document).
These metadata names are generated by using the metamodel classes as follows : QCustomer.customer.firstName.toString() (metamodel generated from sample project http://spring.io/guides/gs/accessing-data-mongodb/).
Support in sorting through embedded documents would also be welcomed, note that special care must be taken because QueryDSL creates intermediate "useless" classes that must be ignored when creating the real path of the desired field to be used


Affects: 1.6 GA (Evans)

Referenced from: pull request #236

Backported to: 1.6.2 (Evans SR2)

1 votes, 4 watchers

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Nov 4, 2014

Thomas Darimont commented

Hi Jordi,

thanks for reporting this. I gave this a quick spin - but I couldn't reproduce the issue.
Would you mind having a look at the PR and adding a small test case that reproduces your issue?

Cheers,
Thomas

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Nov 4, 2014

Jordi Llach Fernandez commented

Hi Thomas,
the problem arises when using QueryDslMongoRepository.findAll(Predicate predicate, Pageable pageable), I think that when this execution path is used there is no way to build a correct Pageable that also sorts.

The point is that Pageable contains the pagination, a Sort object build with a Direction and a bunch {String}}s (properties) in the end, so I can not pass an OrderSpecifier there, so I came up and try to specify these list of properties(at least this is what I was expecting :) ) build from QXXX generated by QueryDSL. Example below :

QPerson person = QPerson.person;
PageRequest pageable = new PageRequest(0, 3, Sort.Direction.ASC, person.address.street.toString());
repository.findAll(person.firstname.isNotNull(), pageable)

I was expecting that org.springframework.data.mongodb.repository.support.SpringDataMongodbSerializer could deal with this way of specifying properties because it exposes this method signature QueryDslMongoRepository.findAll(Predicate predicate, Pageable pageable) that forces me to specify the field in a "String way"

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Nov 6, 2014

Jordi Llach Fernandez commented

I've created the PR #237

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Nov 6, 2014

Thomas Darimont commented

Hi Jordi,

sorry - JIRA wasn't available for some time... I added another example to my PR. I think the class you are looking for is QSort.

Cheers,
Thomas

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Nov 7, 2014

Jordi Llach Fernandez commented

Hi Thomas,
ups O_O, and ups again :) Then maybe the spring-data-mongodb documentation should give a hint on this, also Javadoc
http://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#mongodb.repositories.queries.type-safe

I've also updated my PR but I still does not work(relevant code here)

QSort sorter = new QSort(person.address.street.desc());
PageRequest pageable = new PageRequest(0, 10, sorter);
List<Person> wrongResults = Lists.newArrayList(repository.findAll(person.firstname.isNotNull(), pageable));

Thank you so much and nice to meet you

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Nov 10, 2014

Thomas Darimont commented

Hi Jordi,

it was a pleasure to meet you too :)
What exactly isn't working? The sorting? How does the generated mongodb query look like?

Cheers,
Thomas

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Nov 11, 2014

Jordi Llach Fernandez commented

Hi Thomas,
exactly, the sorting when paginating does not work.

Below is the output when the query is executed as expected, sorting descending by address.street

nov 11, 2014 4:15:34 PM com.mongodb.DBCollectionImpl trace
INFORMACIÓN: find: repositories.person { "$query" : { "firstname" : { "$exists" : true}} , "$orderby" : { "address.street" : -1}}

And the following output when the query is executed with PageRequest and QSort

nov 11, 2014 4:16:02 PM com.mongodb.DBCollectionImpl trace
INFORMACIÓN: find: repositories.person { "$query" : { "firstname" : { "$exists" : true}} , "$orderby" : { "street" : -1}}
nov 11, 2014 4:16:02 PM com.mongodb.DBCollectionImpl trace
INFORMACIÓN: find: repositories.$cmd { "count" : "person" , "query" : { "firstname" : { "$exists" : true}}}

Cheers,

Jordi

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Nov 13, 2014

Thomas Darimont commented

Hi Jordi,

I updated my PR would you mind giving it another spin?

Cheers,
Thomas

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Nov 14, 2014

Jordi Llach Fernandez commented

Hi Thomas,

unfortunately I was unable to make it work in my project ... I think it's due to dependencies, snapshots and so on ...
But I ported some of my model to your branch to make some tests and everything is working fine

Expecting to see this ticket released to check if my problem is related to dependencies or not

Cheers,
Jordi

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Nov 26, 2014

Thomas Darimont commented

Please revise

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Nov 28, 2014

Oliver Drotbohm commented

Would you mind rebasing it on current master?

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Dec 1, 2014

Thomas Darimont commented

Rebased on current master

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants