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

QueryDSL does not work with DBRef [DATAMONGO-362] #1294

Closed
spring-projects-issues opened this issue Dec 26, 2011 · 10 comments
Closed

QueryDSL does not work with DBRef [DATAMONGO-362] #1294

spring-projects-issues opened this issue Dec 26, 2011 · 10 comments
Assignees
Labels
in: mapping Mapping and conversion infrastructure type: enhancement A general enhancement

Comments

@spring-projects-issues
Copy link

Raman Gupta opened DATAMONGO-362 and commented

When an object refers to another object with a @DbRef, QueryDSL cannot be used to navigate "across" the @DbRef. For example:

@Document
Obj1 {
  @DbRef Obj2 obj2Ref;
}

Obj2 {
  String foo;
}

obj1Repo.findOne(QObj1.obj1.obj2Ref.foo.eq("foovalue")) returns null


Affects: 1.0.1, 1.1 M1

Issue Links:

  • DATAMONGO-1848 Migrate to Document API-based Querydsl implementation
    ("depends on")

Referenced from: pull request #13

2 votes, 7 watchers

@spring-projects-issues
Copy link
Author

Raman Gupta commented

Also fails with 1.0-GA

@spring-projects-issues
Copy link
Author

Xavier Vdb commented

I think it will not be easy... mongodb is not relational but it's possible to do that with MapReduce

@spring-projects-issues
Copy link
Author

Raman Gupta commented

Yeah, I think this support has to be added into QueryDSL first, then Spring Data MongoDB can use it... I created querydsl/querydsl#113 to propose this enhancement to the QueryDSL team

@spring-projects-issues
Copy link
Author

Patryk Wąsik commented

This is already implemented in QueryDSL and I can add this to spring data mongo but, I can't figure how.
If we want to have this syntax:

obj1Repo.findOne(QObj1.obj1.obj2Ref.foo.eq("foovalue"))

then we have to rewrite Prediction to diffrent query syntax in QueryDSL with is based on joints, and looke like this:

query.join(qObj1.obj2Ref,obj2Ref).on(obj2Ref.foo.eq("foovalue"))

but this is simply example and prediction can be more complex, so rewriting internaly Prediction to QueryDSL syntax can be pretty complicated.

Another solution is to allowing pass QueryDSL Joints to repository method but this is not possible, because this will have to change QueryDslPredicateExecutor interface declaration.

Anyone have any idea?

@spring-projects-issues
Copy link
Author

Timo Westkämper commented

What do you think about the explicit joins? It makes the extra queries more explicit and is also for JPQL the preferred way to express joins.

You can create your Querydsl queries also directly in Spring Data, so this is just a limitation in the convenience layer

@spring-projects-issues
Copy link
Author

Patryk Wąsik commented

I made initial pull request #13 to solve this

@spring-projects-issues
Copy link
Author

Christoph Strobl commented

Resolved via DATAMONGO-1848. SpringDataMongodbSerializer now exposes join.

{code:java|title = usage sample}
new SpringDataMongodbQuery<>(mongoTemplate, Person.class)
.where()
.join(QPerson.person.coworker, QUser.user)
.on(QUser.user.username.eq("chewbacca"))
.fetch();

@spring-projects-issues
Copy link
Author

Luis Miguel Ospina commented

Christoph Strobl is there any way to use SpringDataMongodbQuery with a pageable instance?

Fetch will return a List and there's no such method: .with(pageable)

As it is in the Query class

@spring-projects-issues
Copy link
Author

Christoph Strobl commented

@Luis Miguel Ospina Please open a new issue for concerns not directly related to the actual ticket so that others who are interested can find it easily. Thank you!

com.querydsl.core.SimpleQuery offers limit and offset. So you can use those for limiting the result.

// ...
.limit(page.getPageSize())
.offset(page.getOffset());

In case you want your method to return a Page instead of List, you can simply use the repository or create your own Page via PageableExecutionUtils.

@spring-projects-issues
Copy link
Author

Rasool Nasr commented

how to use orderBy method of query?

orderBy works with main entity very well.but when I use inner field of dbref entity it doesn't work.

 

for example:
OrderSpecifier<String> order = new OrderSpecifier<String>(Order.ASC,QUser.user.username);
new SpringDataMongodbQuery<>(mongoTemplate, Person.class)
.where()
.join(QPerson.person.coworker, QUser.user)
.on(QUser.user.username.eq("chewbacca"))
query.orderBy(order);
.fetch();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: mapping Mapping and conversion infrastructure type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants