本文介绍了Spring MongoRepository @Query JSONParseException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 MongoDB 实体:

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;
}

我尝试获取所有 Player 的匹配项.这意味着例如我有当前玩家并且匹配列表应该在 playerOne == current player playerTwo == 时返回匹配当前玩家.我为此使用了 MongoRepository:

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);
}

当我执行 findByPlayerId 方法时,我检索到以下错误:

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

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

我注意到错误消息末尾有奇怪的 0 字符:"0}]}

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

我也做了一些变通方法,并传递相同的 player.id 作为第二个方法参数,它工作正常:

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);

您知道为什么第一种方法会返回 JSONParseException 吗?

Do you have any ideas why first approach returns JSONParseException?

推荐答案

这是涵盖该更改的票证.它已解决并发布.尝试启动 1.5.2 及之后版本或 spring mongo 1.10.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.

这篇关于Spring MongoRepository @Query JSONParseException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 15:24