簡體   English   中英

使用PageRequest查找所有文檔時出現spring-data-couchbase錯誤

[英]spring-data-couchbase error while finding all documents with PageRequest

使用Spring Data Couchbase,我創建了一個非常簡單的存儲庫

public interface UserDao extends PagingAndSortingRepository<User, String>

這應該允許我執行分頁的findAll,如下所示:

Page<User> userResult = repo.findAll(new PageRequest(1, 20));

但是,始終拋出以下內容:

Exception in thread "main" java.lang.IllegalStateException: Unknown query param: Page request [number: 1, size 20, sort: null]
at org.springframework.data.couchbase.repository.query.ViewBasedCouchbaseQuery.execute(ViewBasedCouchbaseQuery.java:47)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:337)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.couchbase.repository.support.ViewPostProcessor$ViewInterceptor.invoke(ViewPostProcessor.java:80)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
at $Proxy14.findAll(Unknown Source)
at com.polycom.cloudAxis.proxymanagement.model.Main.main(Main.java:41)

如果創建查詢並使用skip / limit / startKeyDocId,但是如果可能的話,希望使用PagingAndSortingRepository,這不會發生。

知道有什么問題嗎? 感謝所有友好的幫助:)

我也遇到了同樣的問題,這就是我采取的方法。 它實際上對核心實現(目前僅支持Query對象類型)進行了很小的更改,基本上我所做的就是添加另一個check實例:

 if (param instanceof Query) {
            query = (Query) param;
        } else if (param instanceof Pageable) {
            pageable = (Pageable) param;
        }

然后在此處添加了用於分頁的邏輯,到目前為止,它一直在工作,但是我還沒有完全對其進行審查,因此,感謝社區提供的任何意見。

 if (pageable != null) {

        CouchbaseClient client = operations.getCouchbaseClient();
        View view = client.getView(designDocName(), viewName());

        // Paginator p = new Paginator(client, view, query,
        // pageable.getPageSize());
        Paginator paginator = client.paginatedQuery(view, query, pageable.getPageSize());
        // now we need to goto the start point
        ViewResponse viewResponse = null;
        // views are 0 base
        int i = 0;
        while (paginator.hasNext()) {
            viewResponse = paginator.next();
            if (pageable.getPageNumber() == i++) {
                LOGGER.debug("Found the response for this page: {} ", i);
                break;
            }
        }

        if (viewResponse == null) {
            LOGGER.debug("no view response so leaving now");
            return null;
        }
        Class<?> type = method.getEntityInformation().getJavaType();

        final List result = new ArrayList(viewResponse.size());
        for (final ViewRow row : viewResponse) {
            result.add(operations.findById(row.getId(), type));
        }
        return result;

    } 

為了連接起來,我不得不做一些我不想做的事情:D,我只想重寫一種方法,但是要實現它,我需要重寫許多其他事情,所以我最終復制了一些代碼,理想情況下我想將此添加為https://jira.spring.io/browse/DATACOUCH-93的一部分

整個隱含在這里:

public class DCORepositoryFactory extends CouchbaseRepositoryFactory {

CouchbaseOperations                                                                 couchbaseOperations;
MappingContext<? extends CouchbasePersistentEntity<?>, CouchbasePersistentProperty> mappingContext;

public DCORepositoryFactory(CouchbaseOperations couchbaseOperations) {
    super(couchbaseOperations);
    mappingContext = couchbaseOperations.getConverter().getMappingContext();
    this.couchbaseOperations = couchbaseOperations;
}

@Override
protected Object getTargetRepository(RepositoryMetadata metadata) {
    // TODO Auto-generated method stub
    CouchbaseEntityInformation<?, Serializable> entityInformation = getEntityInformation(metadata.getDomainType());
    final DCORepository simpleCouchbaseRepository = new DCORepository(entityInformation, couchbaseOperations);

    simpleCouchbaseRepository.setViewMetadataProvider(ViewPostProcessor.INSTANCE.getViewMetadataProvider());
    return simpleCouchbaseRepository;
}

@Override
protected QueryLookupStrategy getQueryLookupStrategy(QueryLookupStrategy.Key key) {
    return new CouchbaseQueryLookupStrategy();
}

/**
 * Currently, only views are supported. N1QL support to be added.
 */
private class CouchbaseQueryLookupStrategy implements QueryLookupStrategy {
    @Override
    public RepositoryQuery resolveQuery(Method method, RepositoryMetadata metadata, NamedQueries namedQueries) {
        CouchbaseQueryMethod queryMethod = new CouchbaseQueryMethod(method, metadata, mappingContext);
        return new PagingViewBasedCouchbaseQuery(queryMethod, couchbaseOperations);
    }
}

private static class PagingViewBasedCouchbaseQuery extends ViewBasedCouchbaseQuery {

    private static final org.slf4j.Logger   LOGGER  = org.slf4j.LoggerFactory
                                                            .getLogger(DCORepositoryFactory.PagingViewBasedCouchbaseQuery.class);

    private CouchbaseOperations             operations;
    private CouchbaseQueryMethod            method;

    public PagingViewBasedCouchbaseQuery(CouchbaseQueryMethod method, CouchbaseOperations operations) {
        super(method, operations);
        this.operations = operations;
        this.method = method;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.springframework.data.couchbase.repository.query.
     * ViewBasedCouchbaseQuery#execute(java.lang.Object[]) added the ability
     * to support paging
     */
    @Override
    public Object execute(Object[] runtimeParams) {
        Query query = null;
        Pageable pageable = null;
        for (Object param : runtimeParams) {
            if (param instanceof Query) {
                query = (Query) param;
            } else if (param instanceof Pageable) {
                pageable = (Pageable) param;
            } else {
                throw new IllegalStateException(
                        "Unknown query param: (btw null is also not allowed and pagable cannot be null) " + param);
            }
        }

        if (query == null) {
            query = new Query();
        }
        query.setReduce(false);
        if (pageable != null) {

            CouchbaseClient client = operations.getCouchbaseClient();
            View view = client.getView(designDocName(), viewName());

            // Paginator p = new Paginator(client, view, query,
            // pageable.getPageSize());
            Paginator paginator = client.paginatedQuery(view, query, pageable.getPageSize());
            // now we need to goto the start point
            ViewResponse viewResponse = null;
            // views are 0 base
            int i = 0;
            while (paginator.hasNext()) {
                viewResponse = paginator.next();
                if (pageable.getPageNumber() == i++) {
                    LOGGER.debug("Found the response for this page: {} ", i);
                    break;
                }
            }

            if (viewResponse == null) {
                LOGGER.debug("no view response so leaving now");
                return null;
            }
            Class<?> type = method.getEntityInformation().getJavaType();

            final List result = new ArrayList(viewResponse.size());
            for (final ViewRow row : viewResponse) {
                result.add(operations.findById(row.getId(), type));
            }
            return result;

        } else {
            return operations.findByView(designDocName(), viewName(), query, method.getEntityInformation()
                    .getJavaType());
        }
    }

    /**
     * Returns the best-guess design document name.
     *
     * @return the design document name.
     */
    private String designDocName() {
        if (method.hasViewAnnotation()) {
            return method.getViewAnnotation().designDocument();
        } else {
            return StringUtils.uncapitalize(method.getEntityInformation().getJavaType().getSimpleName());
        }
    }

    /**
     * Returns the best-guess view name.
     *
     * @return the view name.
     */
    private String viewName() {
        if (method.hasViewAnnotation()) {
            return method.getViewAnnotation().viewName();
        } else {
            return StringUtils.uncapitalize(method.getName().replaceFirst("find", ""));
        }
    }
}

@Override
protected Class<?> getRepositoryBaseClass(RepositoryMetadata repositoryMetadata) {
    return DCORepository.class;
}

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM