2013-07-13 4 views
0

DBRef를 사용하고 싶지 않습니다. 나는 데이터베이스를 다음과 같이합니다 : 다른 학교 "school1 - 학생" MongoRepository를 사용하여 동적 컬렉션에서 CRUD를 사용하는 방법

  • 컬렉션 이름 "school2 - 학생"
  • 컬렉션 이름 "school3 - 학생"

    1. 컬렉션 이름처럼 자신의 컬렉션이 있습니다. .....

    모든 컬렉션은 학생 정보를 저장하는 데 사용됩니다.

    @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); 
        } 
    } 
    
    관련 문제