且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

“没有找到类型的属性"......当将 QueryDslPredicateExecutor 与 MongoDB 和 Spring-Data 一起使用时

更新时间:2023-09-05 17:32:28

我最终通过扩展和实现 QueryDslPredicateExecutor 而不是更高级别的存储库来解决这个问题.

I ended up solving this by having my base repository extend and implement the QueryDslPredicateExecutor, rather than the higher level repository.

// Custom repository interface
@NoRepositoryBean
public interface ExtendedMongoRepository<T, ID extends Serializable> extends MongoRepository<T, ID>, QueryDslPredicateExecutor<T>{

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

}


// Custom Repository Implementation
public abstract class ExtendedMongoRepositoryImpl<T, ID extends Serializable> extends QueryDslMongoRepository<T, ID>
        implements ExtendedMongoRepository<T, ID> {

    private Class<T> clazz;
    private MongoOperations mongoOperations;
    @SuppressWarnings("unused")
    private MongoEntityInformation<T, ID> metadata;

    public ExtendedMongoRepositoryImpl(MongoEntityInformation<T, ID> metadata, MongoOperations mongoOperations) {
        super(metadata, mongoOperations);
        this.mongoOperations = mongoOperations;
        this.clazz = metadata.getJavaType();
        this.metadata = metadata;
    }

    @Override
    public Page<T> query(Query query, Pageable pageable) {
        List<T> list =  mongoOperations.find(query.with(pageable), clazz);
        return new PageImpl<T>(list, pageable, list.size());
    }
}  

// Entity Repository Interface
public interface TreeRepository extends ExtendedMongoRepository<Tree, String> {}