[英]Spring data rest method not able to search on DbRef Id field
我在应用程序中使用mongodb和spring boot。 在Area集合中,城市是DBRef,但是spring数据保留方法无法使用城市的ID进行搜索。 它返回空的JSON,尽管对于那个城市,我的数据库中有区域。我的Area集合和存储库如下。
@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);
其他集合中的同类关系是工作文件。
您可以添加@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);
您可以尝试按城市搜索吗
@Autowired
private CityRepository cityRepository;
List<Area> findByCity(cityRepository.findByCityId(cityId));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.