Spring Data Mongo Repository ::所有Repo问题的通用共享方法

时间:2012-04-21 16:22:50

标签: mongodb spring-data

使用案例

我正在尝试使用将自定义行为添加到Spring Data MongoDB的所有存储库功能。

文档无益地描述了如何使用JPA进行连接。无论如何使用Mongo等效的配置设置。

我想将findByCategoryName(String categoryName)方法添加到所有实体,因为我的所有实体都有一个Category。 Category是DBRef对象,因此必须使用自定义查询。

以下是配置的相关部分

<!-- Activate Spring Data MongoDB repository support -->
<mongo:repositories base-package="com.domain.*.repo" repository-impl-postfix="CustomImpl" 
    factory-class="com.domain.commonrepo.CommonMongoRepoFactoryBean"/>

<bean id="mappingContext" class="org.springframework.data.mongodb.core.mapping.MongoMappingContext" />

<mongo:mapping-converter mapping-context-ref="mappingContext">
    <mongo:custom-converters base-package="com.domain.mongo.converter" />
</mongo:mapping-converter>

<bean id="entityInformationCreator" class="org.springframework.data.mongodb.repository.support.DefaultEntityInformationCreator">
    <constructor-arg name="mappingContext" ref="mappingContext" />
</bean>

FactoryBean

    @NoRepositoryBean
    public class CommonMongoRepoFactoryBean<T extends MongoRepository<?,?>, ID extends        
    Serializable> extends MongoRepositoryFactoryBean{

@Autowired
private static MongoTemplate mongoTemplate;

protected MongoRepositoryFactory getRepositoryFactory(Class<T> clazz) {
    return new CommonMongoRepoFactory(clazz);
}

private static class CommonMongoRepoFactory extends MongoRepositoryFactory {
    private Class clazz;

    public CommonMongoRepoFactory(Class clazz) {
        super(mongoTemplate);
        this.clazz = clazz;
    }

    public CommonMongoRepoImpl getTargetRepository() {
        return new CommonMongoRepoImpl(clazz);
    }

    public Class<?> getRepositoryClass() {
        return CommonMongoRepoImpl.class;
    }
}

我知道这有点像黑客,但没有文件,这很痛苦。如果有人知道更好,请给我一个github链接: - )

通用回购接口

    @NoRepositoryBean
    public interface CommonMongoRepo<T, ID extends Serializable> extends MongoRepository<T,ID> {

public List<T> findByCategoryName(String categoryName);        

实施

    @NoRepositoryBean
    public class CommonMongoRepoImpl<T, ID extends Serializable> extends SimpleMongoRepository<T,    
    ID> implements CommonMongoRepo<T, ID> {

private Class<T> type;

@Autowired
private static MongoTemplate mongoOperations;

@Autowired
private static EntityInformationCreator entityInformationCreator;

@Autowired
private CategoryRepo categoryRepo;

public CommonMongoRepoImpl(Class<T> type) { 
    super((MongoEntityInformation<T, ID>) entityInformationCreator.getEntityInformation(type), mongoOperations);
}

@Override
public List<T> findByCategoryName(String categoryName) {

    Category category = categoryRepo.findByName(categoryName);

    return mongoOperations.find(query(where("categories.$id").is(category.getId())), type);
}

问题

现在,当我尝试使用常用方法时,我得到了一个异常

在“实体”中找不到属性类别。当mongo repo试图自动实现该方法时,我猜是哪个。这是因为我将bean声明为@NoRepositoryBean

请帮助!!!不想为所有实体添加相同的自定义方法

2 个答案:

答案 0 :(得分:7)

这是最好的解决方案!

第一步:
添加自定义方法到界面!
增加一个自定义的方法

#custom interface

/**
 * Basic Repository for common custom methods
 * @author liangping
 */

import java.io.Serializable;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.data.repository.PagingAndSortingRepository;

@NoRepositoryBean
public interface WootideRepositoryCustom <T, ID extends Serializable>
      extends PagingAndSortingRepository<T, ID>, MongoRepository<T, ID> {

      public Page<T> search(Query query, Pageable pageable);
}

<强>实施

第二步:
为自定义方法添加工具!
实现你的自定义方法

/**
 * implement for wootide basic repository
 * @author liangping
 */

import java.io.Serializable;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.repository.query.MongoEntityInformation;
import org.springframework.data.mongodb.repository.support.SimpleMongoRepository;

public class WootideRepositoryImpl<T, ID extends Serializable> extends
        SimpleMongoRepository<T, ID> implements WootideRepositoryCustom<T, ID> {

    public WootideRepositoryImpl(MongoEntityInformation<T, ID> metadata,
            MongoOperations mongoOperations) {
        super(metadata, mongoOperations);
    }

    @Override
    public Page<T> search(Query query, Pageable pageable) {
        long total = this.getMongoOperations().count(query, this.getEntityInformation().getJavaType() );
        return new PageImpl<T>(this.getMongoOperations().find(query.with(pageable), this.getEntityInformation().getJavaType()), pageable, total);
    }

}

为自定义存储库

创建新工厂
/**
 * Repository Factory for all Subrepository
 * @author liangping
 */

import java.io.Serializable;

import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.mapping.BasicMongoPersistentEntity;
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.query.MongoEntityInformation;
import org.springframework.data.mongodb.repository.support.MappingMongoEntityInformation;
import org.springframework.data.mongodb.repository.support.MongoRepositoryFactory;
import org.springframework.data.mongodb.repository.support.MongoRepositoryFactoryBean;
import org.springframework.data.repository.core.RepositoryMetadata;
import org.springframework.data.repository.core.support.RepositoryFactorySupport;
import org.springframework.data.util.ClassTypeInformation;
import org.springframework.data.util.TypeInformation;

public class WootideRepositoryFactoryBean<R extends MongoRepository<T, I>, T, I extends Serializable>
        extends MongoRepositoryFactoryBean<R, T, I> {

    @Override
    protected RepositoryFactorySupport getFactoryInstance(
            MongoOperations operations) {
        return new WootideMongoRepositoryFactory<T,I>( operations );
    }

    private static class WootideMongoRepositoryFactory<T, ID extends Serializable>
            extends MongoRepositoryFactory {

        private MongoOperations mongo;
        public WootideMongoRepositoryFactory(MongoOperations mongoOperations) {
            super(mongoOperations);
            this.mongo = mongoOperations;
        }

        @SuppressWarnings("unchecked")
        protected Object getTargetRepository(RepositoryMetadata metadata) {

            TypeInformation<T> information =  ClassTypeInformation.from((Class<T>)metadata.getDomainType());
            MongoPersistentEntity<T> pe = new BasicMongoPersistentEntity<T>(information);
            MongoEntityInformation<T,ID> mongometa = new MappingMongoEntityInformation<T, ID>(pe);

            return new WootideRepositoryImpl<T, ID>( mongometa,  mongo);
        }

        protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) {
            return WootideRepositoryCustom.class;
        }
    }
}

让它有效

<mongo:repositories base-package="com.***.mongodb" 
factory-class="com.***.mongodb.custom.WootideRepositoryFactoryBean"/>
祝你好运!祝你好运!

答案 1 :(得分:0)

有点延迟,但here是为Spring网络应用项目执行此操作的示例代码。重点是:

  1. Controller中使用的接口
  2. 在从基础继承的单独类中完成的实现
  3. 基本实现提供了常见的方法,任何其他Controller只能使用快速继承