我不想使用DBRef。我想要一个这样的数据库:不同的学校有自己的收藏
每个集合都用来保存学生的信息。
如我所知,我们可以使用@Document(collection = "school4")或使用MongoTemplate操作来管理集合名称。但是,我想使用MongoRepository。如果有人能帮我的话,我会很感激的。
发布于 2013-07-28 04:09:40
像这样的事情应该有效:
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);
}
}
https://stackoverflow.com/questions/17631754
复制相似问题