-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Description
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