我正在尝试使用 Spring Boot 和 Spring Data Couchbase 项目创建一个简单的原型。到目前为止,我一直在尝试使用 Spring-Data 的查询派生机制从方法名称构建 N1QL 查询。
这就是我的存储库接口定义,问题出在 findBy... 行。
public interface MetricsRepository extends CrudRepository<Single, String> {
Single save(Single entity);
Single findOne(String id);
List<Single> findByServiceID(long serviceId);
}
如果我排除该方法定义,应用程序将毫无问题地启动。如果我包含它,由于以下错误,无法创建存储库 bean:
Caused by: java.lang.AbstractMethodError: org.springframework.data.couchbase.repository.support.CouchbaseRepositoryFactory$CouchbaseQueryLookupStrategy.resolveQuery(Ljava/lang/reflect/Method;Lorg/springframework/data/repository/core/RepositoryMetadata;Lorg/springframework/data/repository/core/NamedQueries;)Lorg/springframework/data/repository/query/RepositoryQuery;
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:416)
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:206)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:251)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:237)
at org.springframework.data.couchbase.repository.support.CouchbaseRepositoryFactoryBean.afterPropertiesSet(CouchbaseRepositoryFactoryBean.java:96)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
... 36 more
如果我指定一个@Query(例如:@Query("#{#n1ql.selectEntity} WHERE role = $1") 我得到同样的错误。
我的实体定义:
import com.couchbase.client.java.repository.annotation.Id;
import com.couchbase.client.java.repository.annotation.Field;
import org.springframework.data.couchbase.core.mapping.Document;
@Document
public class Single {
@Id private final String eventID;
@Field private final long serviceID;
@Field private final long metric;
@Field private final long timestamp;
@Field private final long previous;
public Single(String eventID, long serviceID, long metric, long timestamp, long previous) {
this.eventID = eventID;
this.serviceID = serviceID;
this.metric = metric;
this.timestamp = timestamp;
this.previous = previous;
}
public String getEventID() { return eventID; }
public long getServiceID() { return serviceID; }
public long getMetric() { return metric; }
public long getTimestamp() { return timestamp; }
public long getPrevious() { return previous; }
}
我通过 REST 控制器中的 @Autowired 注释使用存储库。我有一个@Configuration 和@EnableCouchbaseRepositories 配置类@Import-ed 到@SpringBootApplication。我的测试实例上安装了 Couchbase Server 4.0.0 社区版本,如果没有 n1ql 查询,我可以连接、存储和检索实体。
我在 gradle 中的依赖项:
dependencies {
compile("org.springframework.data:spring-data-couchbase:2.1.1.RELEASE")
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.springframework.boot:spring-boot-starter-actuator")
testCompile("org.springframework.boot:spring-boot-starter-test")
testCompile("junit:junit")
}