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

@Id annotated attribute of type BigInteger does not work with query methods [DATAMONGO-1445] #2356

Closed
spring-projects-issues opened this issue Jun 3, 2016 · 2 comments
Assignees
Labels
in: mapping Mapping and conversion infrastructure type: bug A general bug

Comments

@spring-projects-issues
Copy link

spring-projects-issues commented Jun 3, 2016

Daniele Demichelis opened DATAMONGO-1445 and commented

I have a simple model, made up by three classes: Author, Book and Movie. Both Book and Movie have an author field.

Here's the Author.

@Document(collection = "authors")
@TypeAlias("author")
public class Author {

    @Id
    private ObjectId id;
    private String firstName;
    private String lastName;
...

Here's the Book. @Id is a BigInteger.

@Document(collection = "books")
@TypeAlias("book")
public class Book {

    @Id private BigInteger id;
    @DBRef(lazy = true) private Author author;
...

Here's the Movie. @Id is a String.

@Document(collection = "movies")
@TypeAlias("movie")
public class Movie {
    @Id
    private String id;

Here are the two repositories.

public interface BookRepository extends PagingAndSortingRepository<Book, BigInteger> {

    public Book findByIsbn(String isbn);

    List<Book> findByAuthor(Author author);

    Book findByIdAndAuthor(BigInteger id, Author authorJohn);

    Book findById(BigInteger id);
}


public interface MovieRepository extends CrudRepository<Movie, String> {

    Movie findByIdAndAuthor(String movieId, Author author);

}

Now, the method Book#findByIdAndAuthor() fails. If you check the logs, it seems also pretty clear why.

MovieRepository#findByIdAndAuthor() prints that. Notes that "_id" is contrained with an "$oid".

2016-06-03 18:29:14 [main] DEBUG o.s.data.mongodb.core.MongoTemplate - findOne using query: { "_id" : { "$oid" : "5751b05ac2c9f73cde7f1800"} , "author" : { "_id" : { "$oid" : "5751b05ac2c9f73cde7f17ff"} , "firstName" : "Pasolini"}} in db.collection: tutorial.movies

While BookRepository#findByIdAndAuthor() prints that. "_id" is not correctly queried with an "$oid".

2016-06-03 18:30:09 [main] DEBUG o.s.data.mongodb.core.MongoTemplate - findOne using query: { "_id" : "27023952671160688784893370221" , "author" : { "$ref" : "authors" , "$id" : { "$oid" : "5751b091c2c9ea16c8115f6b"}}} in db.collection: tutorial.books

Affects: 1.8.4 (Gosling SR4)

Reference URL: https://github.com/danidemi/tutorial-spring-data-mongodb/blob/master/src/test/java/com/danidemi/tutorial/JiraBugDATAMONGO1445.java

@spring-projects-issues
Copy link
Author

spring-projects-issues commented Jun 3, 2016

Daniele Demichelis commented

I wrote a unit test here: https://github.com/danidemi/tutorial-spring-data-mongodb/blob/master/src/test/java/com/danidemi/tutorial/JiraBugDATAMONGO1445.java.
It is @Ignored not to block the build. It's written with Spring Boot, so you have to tweak the properties file to connect to a running MongoDb.

@spring-projects-issues
Copy link
Author

spring-projects-issues commented Jun 6, 2016

Oliver Drotbohm commented

Thanks for the test case, Daniele. I could isolate the error to the Gosling branch in which the query creator erroneously converts the provided values too eagerly which means that the identifier property detection and the special handling they need does not get applied. The good news is: this actually works on Hopper.

I'll have this fixed for the Gosling branch in a bit but we're approaching a Hopper SR2 release tomorrow, so I'll slightly defer working on a fix here

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: bug A general bug
Projects
None yet
Development

No branches or pull requests

2 participants