【问题标题】:QueryDslMongoRepository for different collectionsQueryDslMongoRepository 用于不同的集合
【发布时间】:2013-12-27 16:51:16
【问题描述】:

我正在尝试为模型“文档”实现 QueryDslMongoRepository

@QueryEntity
@Document(collection="currentDocuments")
public class DocumentImpl extends TranslatableObjectImpl implements Document

在我们当前的实现中,要删除的文档将 von "currentDocuments" 移动到 "deletedDocuments" 集合中。

我找不到像这样创建存储库的解决方案

public interface DocumentRepository extends MongoRepository<DocumentImpl, String> ,QueryDslPredicateExecutor<DocumentImpl> {}

具有动态集合名称。

我的目标是在一个存储库中为不同的集合提供 queryDsl 的优势,并能够将模型从一个集合移动到另一个集合中

public move(DocumentImpl entity, String sourceCollection, String targetCollection){
    repository.delete(entity,sourceCollection);
    repository.save(entity,targetCollection);
}

或类似的东西

public List<Document> findAllDocumentsWithAttachments(String collectionName){
    return repository.findAll(QDocumentImpl.documentImpl.attachments.isNotEmpty(), collectionName);
}

有什么建议吗?

【问题讨论】:

    标签: spring mongodb repository spring-data querydsl


    【解决方案1】:

    我通过创建一个自己的 FactoryBean 扩展 MongoRepositoryFactoryBean 来实现此功能。

    【讨论】:

      【解决方案2】:

      根据这个答案 -> Question

      实体

      @QueryEntity
      public class Document extends AbstractObject {
      }
      

      自定义 QuerydslMongoRepository

      public interface CustomQuerydslMongoRepository<T extends AbstractObject,ID extends Serializable> extends MongoRepository<T, ID> ,QueryDslPredicateExecutor<T>{
          List<T> findAll(Predicate predicate, String collectionName);
          T save(T entity, String collectionName);
          ...     
      }
      

      自定义 QuerydslMongoRepository 实现

      public class CustomQuerydslMongoRepositoryImpl<T extends AbstractObject,ID extends Serializable> extends QueryDslMongoRepository<T,ID> implements CustomQuerydslMongoRepository<T,ID> {
      
          //All instance variables are available in super, but they are private
          private static final EntityPathResolver DEFAULT_ENTITY_PATH_RESOLVER = SimpleEntityPathResolver.INSTANCE;
      
      
          private final EntityPath<T> path;
          private final PathBuilder<T> pathBuilder;
          private final MongoOperations mongoOperations;
          public CustomQuerydslMongoRepositoryImpl(MongoEntityInformation<T, ID> entityInformation, MongoOperations mongoOperations) {
              this(entityInformation, mongoOperations,DEFAULT_ENTITY_PATH_RESOLVER);
          }
      
          public CustomQuerydslMongoRepositoryImpl(MongoEntityInformation<T, ID> entityInformation, MongoOperations mongoOperations, EntityPathResolver resolver) {
              super(entityInformation, mongoOperations, resolver);
              this.path=resolver.createPath(entityInformation.getJavaType());
              this.pathBuilder = new PathBuilder<T>(path.getType(), path.getMetadata());
              this.mongoOperations=mongoOperations;
          }
      
          @Override
           public List<T> findAll(Predicate predicate, String collectionName) {
      
          MongodbQuery<T> query = createQueryFor(predicate,collectionName);
          return query.list();
      }
      
          @Override
      public T save(T entity,String collectionName){
          Assert.notNull(entity, "Entity must not be null!");
          mongoOperations.save(entity, collectionName);
          return entity;
      }
      
          private MongodbQuery<T> createQueryFor(Predicate predicate,String collectionName) {
          Class<T> domainType = getEntityInformation().getJavaType();
          MongodbQuery<T> query = new SpringDataMongodbQuery<T>(getMongoOperations(), domainType,collectionName);
          return query.where(predicate);
      }   
      }
      

      自定义存储库工厂

      public class CustomQueryDslMongodbRepositoryFactoryBean<R extends QueryDslMongoRepository<T, I>, T, I extends Serializable> extends MongoRepositoryFactoryBean<R, T, I> {
      
      
          @Override
          protected RepositoryFactorySupport getFactoryInstance(MongoOperations operations) {
              return new CustomQueryDslMongodbRepositoryFactory<T,I>(operations);
          }
      
          public static class CustomQueryDslMongodbRepositoryFactory<T, I extends Serializable> extends MongoRepositoryFactory {
              private MongoOperations operations;
      
              public CustomQueryDslMongodbRepositoryFactory(MongoOperations mongoOperations) {
                  super(mongoOperations);
                  this.operations = mongoOperations;
              }
      
      
              @SuppressWarnings({ "rawtypes", "unchecked" })
              protected Object getTargetRepository(RepositoryMetadata metadata) {
                      return new CustomQuerydslMongoRepositoryImpl(getEntityInformation(metadata.getDomainType()), operations);
                }
      
              protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) {
                  return CustomQuerydslMongoRepository.class;
              }
          }
      }
      

      实体存储库

      public interface DocumentRepository extends CustomQuerydslMongoRepository<Document, String>{
      
      }
      

      在服务中的使用

      @Autowired
      DocumentRepository repository;
      
      public List<Document> getAllDocuments(Predicate predicate){
      return repository.findAll(predicate,"myCustomCollection");
      }
      

      【讨论】:

        猜你喜欢
        • 2011-04-29
        • 1970-01-01
        • 1970-01-01
        • 2021-06-30
        • 2019-10-13
        • 2017-06-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多