0

我正在尝试使用 spring 中的 dbref 函数。

我的代码:

@EnableAutoConfiguration
@RestController
@RequestMapping("/poi")
public class PoiBasicController {
    @Autowired
    private PoiRepository poiRepository;

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public @ResponseBody ResponseEntity<String> add(@RequestBody PointOfInterest poi) {

        poiRepository.insert(poi);

        return ResponseEntity.status(HttpStatus.OK).body(null);
    }

@EnableAutoConfiguration
@RestController
@RequestMapping("/tour")
public class TourController {

    @Autowired
    private TourRepository tourRepository;

    @Autowired
    private PoiRepository poiRepository;

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public @ResponseBody ResponseEntity<String> addTour(@RequestBody Tour tour) {
        tourRepository.insert(tour);
        return ResponseEntity.status(HttpStatus.OK).body(null);
    }

@Document
public class Tour {
    @Id
    private String id;
    private HashMap<String, String> title;
    private HashMap<String, String> description;
    private List<String> images;
    @DBRef(db = "pois")
    private List<PointOfInterest> poiList;

@Document(collection = "pois")
public abstract class PointOfInterest {
    @Id
    private String id;

    private UpperCategory upperCategory;
    private List<String> tags;
    private int priority;
    private List<String> images;
    private LocationWrapper location;
    /*
        Languagekey, description
     */
    private HashMap<String, String> description;

Poi 类由不同类型的 poi 实现(例如 Culture)。我想将我的 Tour 对象中的 pois 引用为列表。

我做一个 POST 以在我的 pois 集合中存储一个 poi。

问题:当我使用 poi 对象的 objectID-reference 对我的游览进行 POST 时,我在游览中的 poi 列表始终为空。

我理解@DBRef 是对的,不是吗?

编辑1:错误消息

问题是:假设您在数据库中有两个 pois,分别用“poi_id_1”和“poi_id_2”引用。现在我开始对 /tour/add 的 API 调用,其中包含以下 JSON 数组(省略其他参数):
"poiList": [{"id": "poi_id_1"}, {"id":"poi_id_2"}]

我得到一个200。

但是:当我开始查询旅行时,我得到"poiList": [null]了结果。(省略其他论点)

提前感谢您的帮助:)

4

1 回答 1

0

您不应该使用该db属性,@DBRef因为您的 POI 驻留在同一个数据库中,并且集合名称是从您的域对象中推断出来的。

通过指定,您要求 Spring Data 完全在不同的数据库@DBRef(db="...")中查找条目。

只需使用@DBref

于 2016-05-30T19:05:07.213 回答