Results 1 to 10 of 10

Thread: MongoDB: Spring-Data-Document, Repository and Querydsl with Id

  1. #1
    Join Date
    Aug 2004
    Posts
    18

    Question MongoDB: Spring-Data-Document, Repository and Querydsl with Id

    Hi

    If I have an object like this

    Code:
    @Document
    public class User {
    	@Id
    	private String id;
    }
    and then I try to do something like this

    Code:
    User user = repository.findOne(QUser.user.id.eq("4e721fba6733246393b1095e"));
    it does not work, because QueryDsl creates a query with 'id' instead of '_id'. Is this working as intended and there is no other way than renaming the id property to _id (private String _id)? Or is there a way to tell QueryDsl that the fieldname is _id on the database?

    With the spring-data-document Query this works fine. The library knows that it has to rename the field to _id for the query.
    mongoTemplate.find(Query.query(Criteria.where("id" ).is("4e721fba6733246393b1095e")), User.class);


    Ralph

  2. #2

    Default

    Hi Ralph,

    I also have the same exact problem when using QueryDSL with MongoDB. Even if I try to change "id" to "_id", it doesn't help.
    Were you been able to find a solution for your problem? If yes I would appreciate if you could share it.

    Thanks,
    Mariam

  3. #3
    Join Date
    Apr 2006
    Location
    Dresden, Germany
    Posts
    483

    Default

    What Spring Data MongoDB and Querydsl versions are you using? For the 1.0 GA release fo SD MongoDB we implemented a just introduced hook in Querydsl to pipe all predicates through our converter. Beyond that our custom serializer actually uses the mapping metadata to calculate field keys so that should be working actually.

    If no, would you mind coming up with a very minimal test case against SpringDataMongodbSerializer?

  4. #4

    Default MongoDB "in" query problem

    Hi Oliver,

    I'm using the latest MongoDB version and 2.3.0 version for QueryDsl. I have written many test cases for different cases, e.g. "person.lastname.eq" case:

    @Test
    public void findsPeopleByQueryDslLastnameSpec() throws Exception {

    Iterable<Person> result = repository.findAll(person.lastname.eq("Matthews")) ;
    assertThat(result, hasItem(dave));
    assertThat(result, not(hasItems(carter, boyd, stefan, leroi, alicia)));
    }

    But we need to use in query, e.g. "person.id.in(Arrays.asList(new String[]{"4f43b6a384aea4e77d403709"}))" and in this case test would look like

    @Test
    public void findsPeopleByQueryDslIDInSpec() throws Exception {

    Iterable<Person> result = repository.findAll(person.id.in(Arrays.asList(new String[]{"4f43b6a384aea4e77d403709", "4f43b6a384aea4e77d403708"})));
    assertThat(result, hasItem(dave));
    }

    and the problem is that in query doesn't work at all.
    Any suggestions/advice would be helpful. Thanks in advance.

    -Mariam


    Quote Originally Posted by Oliver Gierke View Post
    What Spring Data MongoDB and Querydsl versions are you using? For the 1.0 GA release fo SD MongoDB we implemented a just introduced hook in Querydsl to pipe all predicates through our converter. Beyond that our custom serializer actually uses the mapping metadata to calculate field keys so that should be working actually.

    If no, would you mind coming up with a very minimal test case against SpringDataMongodbSerializer?

  5. #5

    Default

    Also would like to mention that

    Iterable<Person> result = repository.findAll(person.lastname.in(Arrays.asLis t(new String[]{"Matthews"})));

    in query works when the search is done other than "id" field.
    But this one doesn't work:

    Iterable<Person> result = repository.findAll(person.id.in(Arrays.asList(new String[]{"4f43b6a384aea4e77d403709", "4f43b6a384aea4e77d403708"})));

  6. #6
    Join Date
    Jun 2012
    Posts
    2

    Default

    I've just come across this issue.

    Using this entity (@Document) definition with a String as the declared ID:

    User.class
    ...
    @Id String id;


    and the following query

    QUser.id.eq("4f43b6a384aea4e77d403709")

    Always returns null for a Repository find.

    Looking at the mongoDb query log (mongod -v) it appears that the spring-data-mongodb/QueryDSL layers are not translating the String "4f43b6a384aea4e77d403709" to ObjectId("4f43b6a384aea4e77d403709") as one would normally do in the mongo shell.

    I Can't pass in an ObjectId manually, because the queryDSL element "id" is a StringPath and typed to String.

    I'm having the inject MongoTemplate into my service layer just to do getById DAO functions.

    How can I provide a test case to show this?

    is there a quick-start archetype I can utilise? or a barebones I can clone?

    Thanks

    Rob

    Mongo Driver: 2.5.3
    Spring framework: 3.1.0-RELEASE
    Spring-data-mongodb: 1.0.1.RELEASE
    QueryDSL-mongodb: 2.3.0

  7. #7
    Join Date
    Apr 2006
    Location
    Dresden, Germany
    Posts
    483

    Default

    This seems to be an issue with our serializer, which doesn't apply the special id handling when serializing the query thorugh Querydsl. Would you please open a JIRA ticket? I'll take care of that then…

  8. #8
    Join Date
    Jun 2012
    Posts
    2

    Default

    Done: https://jira.springsource.org/browse/DATAMONGO-467

    With thanks.

    Please let me know If can help to resolve this. (E.g. patched version testing etc)

  9. #9
    Join Date
    Apr 2006
    Location
    Dresden, Germany
    Posts
    483

    Default

    I think I already know the black spot and will give it a shot early next week. Thanks for filing the ticket!

  10. #10
    Join Date
    Apr 2006
    Location
    Dresden, Germany
    Posts
    483

    Default

    Fix deployed to snapshot repositories.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •