不使用DBRef映射集合(Spring Data MongoDB)

马里亚诺德克

我已经阅读了很多文章,不建议在Spring Data / MongoDB中使用DBRef进行集合映射。那么,如何实现一个映射,该映射存储从学生集合内的那些对象获取的ObjectId数组

假设我有以下POJO模型:

@Document (collection = "courses")
public class Course {

    @Id
    private String id;

    private String name;

    private List<Student> students = new LinkedList<Student>();

    //.. constructors, getters and setters ..
}

public interface CourseRepository extends MongoRepository<Course, String> { }

结果应该是这样的:

courses
{
    _id : ObjectId("foo"),
    _class: "model.Course",
    name: "MongoDB for Dummies",
    students: [ ObjectId("foo2"), ObjectId("foo3"), ... ]
}

代替这个:

courses
{
    _id : ObjectId("foo"),
    _class: "model.Course",
    name: "MongoDB for Dummies",
    students: [ 
             DBRef("student", ObjectId("foo2")),
             DBRef("student", ObjectId("foo3"))
    ]
}

谢谢!

奥利弗·德罗博姆(Oliver Drotbohm)

您可能想尝试显而易见的方法并更改studentsList<ObjectId>;)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章