2013-07-13 97 views
0

我不想使用DBRef。 我想要一個數據庫這樣的: 不同的學校有自己的收藏像如何在動態集合中使用MongoRepository到CRUD

  1. 集合名稱「school1-學生」
  2. 集合名稱「school2-學生」
  3. 集合名稱「school3-學生」。 .....

每個集合都是用來保存學生的信息。

據我所知,我們可以使用@Document(collection =「school4」)或使用MongoTemplate操作來管理集合名稱。但是我想使用MongoRepository。如果有人能幫助我,我將不勝感激。

回答

0

像這樣的東西應該工作:

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); 
    } 
}