Spring MongoRepository @Query JSONParseException

懵懂的女人 提交于 2019-12-25 16:47:23

问题


I have the following MongoDB entities:

public class Player {

    @Id 
    private String id;

    private String username;

    private int rating;

    private boolean active;
}

public class Match {

    @Id
    private String id;

    @DBRef
    private Player playerOne;

    @DBRef
    private Player playerTwo;
}

I try to get all Player's matches. It means that e.g I have current player and matches list should be returned for matches when playerOne == current player or playerTwo == current player. I used MongoRepository for this:

public interface MatchRepository extends MongoRepository<Match, String> {

    @Query(value = "{'$or': [{'playerOne.id': ?0}, {'playerTwo.id': ?0}]}")
    List<Match> findByPlayerId(String playerId);
} 

When I've executed findByPlayerId method I retrieved below error:

Caused by: com.mongodb.util.JSONParseException: {'$or': [{'playerOne.id': "58ea191756a4302290fff9b1"}, {'playerTwo.id': "58ea191756a4302290fff9b1"0}]}

I noticed strange 0 character on the end of error message: "0}]}

I also made some workaround and pass the same player.id as a second method argument and it works fine:

@Query(value = "{'$or': [{'playerOne.id': ?0}, {'playerTwo.id': ?1}]}")
List<Match> findByPlayerId(String playerId, String palyerId2);

Do you have any ideas why first approach returns JSONParseException?


回答1:


Here is the ticket covering that change. Its been resolved and released. Try boot version 1.5.2 and after or spring mongo 1.10.1 and after.

https://jira.spring.io/browse/DATAMONGO-1603.



来源:https://stackoverflow.com/questions/43306460/spring-mongorepository-query-jsonparseexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!