首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在动态集合中使用MongoRepository进行CRUD

如何在动态集合中使用MongoRepository进行CRUD
EN

Stack Overflow用户
提问于 2013-07-13 15:48:33
回答 1查看 690关注 0票数 0

我不想使用DBRef。我想要一个这样的数据库:不同的学校有自己的收藏

  1. 集合名称“学校1-学生”
  2. 馆藏名称“学校2-学生”
  3. 收藏名称“学校3-学生”.

每个集合都用来保存学生的信息。

如我所知,我们可以使用@Document(collection = "school4")或使用MongoTemplate操作来管理集合名称。但是,我想使用MongoRepository。如果有人能帮我的话,我会很感激的。

EN

回答 1

Stack Overflow用户

发布于 2013-07-28 04:09:40

像这样的事情应该有效:

代码语言:javascript
运行
复制
public class PerSchoolStudentRepository {
    public static CrudRepository<Student, ObjectId> buildRepository(String school, MongoOperations mongoOperations) {
        MongoPersistentEntity<Student> persistentEntity = (MongoPersistentEntity<Student>) mongoOperations.getConverter().getMappingContext().getPersistentEntity(Student.class);
        MongoEntityInformation<Student, ObjectId> mongoEntityInformation = new MappingMongoEntityInformation<Student, ObjectId>(persistentEntity, school+"Students");
        return new SimpleMongoRepository<Student, ObjectId>(mongoEntityInformation, mongoOperations);
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17631754

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档