[英]I am using spring-data-couchbase , but the Query creation from method names does not work
I am using spring-data-couchbase , but the Query creation from method names is not work. 我正在使用spring-data-couchbase,但是从方法名称创建查询无效。 following is my code:
以下是我的代码:
spring-couchbase.xml spring-couchbase.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/data/jpa"
xmlns:couchbase="http://www.springframework.org/schema/data/couchbase"
xmlns:jpa="http://www.springframework.org/schema/data/couchbase"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/data/couchbase
http://www.springframework.org/schema/data/couchbase/spring-couchbase.xsd">
<couchbase:couchbase bucket="comment" host="192.168.20.118,192.168.20.71" />
<couchbase:template/>
<jpa:repositories base-package="com.david.comment.core.repository" />
</beans:beans>
Repository: 仓库:
public interface CommentRepository extends PagingAndSortingRepository<CommentDoc, String> {
public Page<CommentDoc> findByUserId(String userId, Pageable pageable);
}
Domain: 域:
@Document
@Data
public class CommentDoc {
@Id
private String id;
/**
* 话题
*/
@Field
private String topicId;
/**
* 用户ID
*/
@Field
private String userId;
}
Test: 测试:
@Test
public void testFindByC1() {
Pageable pageable = new PageRequest(0, 10);
Page<CommentDoc> comments = commentRepository.findByUserId("user01", pageable);
for(CommentDoc commentDoc : comments) {
logger.debug("doc: {}", commentDoc);
}
}
Exception: 例外:
java.lang.IllegalStateException: Unknown query param: user01
at org.springframework.data.couchbase.repository.query.ViewBasedCouchbaseQuery.execute(ViewBasedCouchbaseQuery.java:47)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:393)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:371)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.couchbase.repository.support.ViewPostProcessor$ViewInterceptor.invoke(ViewPostProcessor.java:87)
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 com.sun.proxy.$Proxy24.findByUserId(Unknown Source)
at com.runmit.comment.core.test.CommentRepositoryTest.testFindByC1(CommentRepositoryTest.java:57)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:73)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:73)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:217)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:83)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:68)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:163)
at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)
Please someone help me! 请有人帮我! How can I resolve it.
我该如何解决。
I'm new to this, but just went through it. 我对此并不陌生,但刚刚经历了它。 The Couchbase stuff doesn't seem to support everything offered in the other Spring Data repositories.
Couchbase的东西似乎并不支持其他Spring Data存储库中提供的所有功能。
You have to add a production view called commentdoc/byUserId (in development view design document name = commentdoc and viewname = byUserId). 您必须添加一个名为commentdoc / byUserId的生产视图(在开发视图中,设计文档名称= commentdoc,视图名称= byUserId)。 Don't forget to publish.
不要忘记发布。 Something like (fix classname):
像(fix classname)这样的东西:
function (doc, meta) { if(doc._class == "com.david.XXX.CommentDoc" && doc.userId){ emit(doc.userID, null); } }
Change your repo signature from String userId to Query userId 将您的回购签名从字符串userId更改为Query userId
Pass a Query object. 传递查询对象。 Something like:
就像是:
Query query = new Query(); query.setKey(ComplexKey.of("useridofinteres=")); commentRepository.findByUserId(query, pageable);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.