Java 查找具有PageRequest的所有文档时发生spring data couchbase错误
我使用SpringDataCouchbase创建了一个非常简单的存储库Java 查找具有PageRequest的所有文档时发生spring data couchbase错误,java,spring-data,couchbase,spring-data-couchbase,Java,Spring Data,Couchbase,Spring Data Couchbase,我使用SpringDataCouchbase创建了一个非常简单的存储库 public interface UserDao extends PagingAndSortingRepository<User, String> 如果我创建一个查询并使用skip/limit/startKeyDocId,但如果可能的话,我希望使用PagingAndSortingRepository,则不会发生这种情况 你知道会出什么问题吗?感谢所有的友好帮助:)我也遇到了同样的问题,以下是我采取的方法。这实际
public interface UserDao extends PagingAndSortingRepository<User, String>
如果我创建一个查询并使用skip/limit/startKeyDocId,但如果可能的话,我希望使用PagingAndSortingRepository,则不会发生这种情况
你知道会出什么问题吗?感谢所有的友好帮助:)我也遇到了同样的问题,以下是我采取的方法。这实际上是对核心实现(目前仅支持查询对象类型)的一个非常小的更改,基本上我所做的只是添加另一个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;
}
if(可分页!=null){
CouchbaseClient=operations.getCouchbaseClient();
View=client.getView(designDocName(),viewName());
//Paginator p=新的Paginator(客户端、视图、查询、,
//getPageSize());
Paginator Paginator=client.paginatedQuery(视图,查询,pageable.getPageSize());
//现在我们需要回到起点
ViewResponse ViewResponse=null;
//视图以0为基数
int i=0;
while(paginator.hasNext()){
viewsresponse=paginator.next();
if(pageable.getPageNumber()==i++){
debug(“找到此页面的响应:{}”,i);
打破
}
}
if(viewsresponse==null){
debug(“没有视图响应,所以现在就离开”);
返回null;
}
Class type=method.getEntityInformation().getJavaType();
最终列表结果=新的ArrayList(viewResponse.size());
用于(最终视图行:视图响应){
add(operations.findById(row.getId(),type));
}
返回结果;
}
为了实现这一点,我必须做一些我不想做的事情:D,我只想重写一个方法,但是要实现它,我需要重写许多其他事情,所以我最终复制了一点代码,理想情况下,我希望将其作为
整个过程如下:
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;
}
}
公共类DCORepositoryFactory扩展了CouchbaseRepositoryFactory{
CouchbaseOperations CouchbaseOperations;
MappingContext,CouchbasePersistentProperty>MappingContext;
公共数据存储工厂(CouchbaseOperations CouchbaseOperations){
超级(couchbaseOperations);
mappingContext=couchbaseOperations.getConverter().getMappingContext();
this.couchbaseOperations=couchbaseOperations;
}
@凌驾
受保护对象GetTargetPository(RepositoryMetadata元数据){
//TODO自动生成的方法存根
CouchbaseEntityInformation type=method.getEntityInformation().getJavaType();
最终列表结果=新的ArrayList(viewResponse.size());
用于(最终视图行:视图响应){
add(operations.findById(row.getId(),type));
}
返回结果;
}否则{
返回操作.findByView(designDocName(),viewName(),查询,方法.getEntityInformation()
.getJavaType());
}
}
/**
*返回最佳猜测设计文档名称。
*
*@返回设计文档名称。
*/
私有字符串designDocName(){
if(方法.hasViewAnnotation()){
返回方法.getViewAnnotation().designDocument();
}否则{
返回StringUtils.uncapitalize(方法.getEntityInformation().getJavaType().getSimpleName());
}
}
/**
*返回最佳猜测视图名称。
*
*@返回视图名称。
*/
私有字符串viewName(){
if(方法.hasViewAnnotation()){
返回方法.getViewAnnotation().viewName();
}否则{
返回StringUtils.uncapitalize(method.getName().replaceFirst(“find”,”);
}
}
}
@凌驾
受保护类getRepositoryBaseClass(RepositoryMetadata RepositoryMetadata){
返回DCORepository.class;
}
}
它工作正常,我只是稍微修改了代码,以使用默认存储库(SimpleTouchBaseRepository)而不是DCORository,并返回一个页面对象而不是普通列表。要使其返回页面,只需将可分页“if”的最后一位修改为:final List result=new ArrayList(viewsresponse.size());for(final ViewRow row:viewResponse){result.add(operations.findById(row.getId(),type));}返回新的PageImpl(result,pageable,viewResponse.getTotalRows());这实际上是一个很好的观点。我必须检查spring数据中分页的正常行为。我们可以使用.getReturnedObjectType()方法并根据接口的定义返回适当的对象,以返回集合或页面对象。
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;
}
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;
}
}