2017-11-16 1 views
0

私の申請でmongodbとspring bootを使用しています。エリアコレクションでは都市はDBRefですが、春のデータ休憩方法は都市のIDを使用して検索できません。その都市では私のデータベースにエリアがありますが、空のJSONを返しています。私のエリアコレクションとリポジトリは次のとおりです。DbRef IDフィールドで春データ休止メソッドを検索できません

@Document(collection = "area") 
    public class Area { 

     private String name; 

     private String areaCode; 

     private String postalCode; 

     private String latitude; 

     private String longitude; 

     private String category; 

     @DBRef(lazy = false) 
     private City city; 

public interface AreaRepo extends MongoRepository<Area, String> { 

    @RestResource(path = "byCityId") 
    List<Area> findByCityId(@Param(value = "cityId") String cityId); 

他のコレクションと同じ種類の関係が作業ファイルです。

答えて

1

@Queryアノテーションを追加できます。

@Query("{ 'city': {'$ref': 'City', '$id': { '$oid': ?0 } } }") 
List<Area> findByCityId(@Param(value = "cityId") String cityId); 

や都市オブジェクト

City city = new City(cityId); 
List<Area> findByCity(City city); 
+0

感謝を検索してみてくださいでしたが、私はエラーの下に取得しています。 原因:java.lang.IllegalArgumentException:ObjectIdの無効な16進表現:[_param_0] –

0

と直接見つけるあなたは市があなたの応答のための

@Autowired 
private CityRepository cityRepository; 

List<Area> findByCity(cityRepository.findByCityId(cityId)); 
関連する問題