4

我想将 JPA 用于 micronaut。为此,我正在使用io.micronaut.data:micronaut-data-hibernate-jpa:1.0.0.M1库。每当我运行我的应用程序并点击端点以获取数据时,我都会收到以下错误:

{
    message: "Internal Server Error: No backing RepositoryOperations configured for repository. Check your configuration and try again"
} 

我尝试查找错误,但找不到。在此处附加我的文件。请帮忙。

构建.gradle

plugins {
    id "net.ltgt.apt-eclipse" version "0.21"
    id "com.github.johnrengelman.shadow" version "5.0.0"
    id "application"
}



version "0.1"
group "micronaut.test"

repositories {
    mavenCentral()
    maven { url "https://jcenter.bintray.com" }
}

configurations {
    // for dependencies that are needed for development only
    developmentOnly 
}

dependencies {
    annotationProcessor platform("io.micronaut:micronaut-bom:$micronautVersion")
    annotationProcessor "io.micronaut:micronaut-inject-java"
    annotationProcessor "io.micronaut:micronaut-validation"
    annotationProcessor "org.projectlombok:lombok:1.16.20"
    annotationProcessor 'io.micronaut.data:micronaut-data-processor:1.0.0.M1'

    implementation platform("io.micronaut:micronaut-bom:$micronautVersion")
    compile 'io.micronaut.data:micronaut-data-hibernate-jpa:1.0.0.M1'
    implementation "io.micronaut:micronaut-inject"
    implementation "io.micronaut:micronaut-validation"
    implementation "io.micronaut:micronaut-runtime"
    implementation "io.micronaut:micronaut-http-server-netty"
    implementation "io.micronaut:micronaut-http-client"
    implementation 'nl.topicus:spanner-jdbc:1.1.5'



    runtimeOnly "ch.qos.logback:logback-classic:1.2.3"
    testAnnotationProcessor platform("io.micronaut:micronaut-bom:$micronautVersion")
    testAnnotationProcessor "io.micronaut:micronaut-inject-java"
    testImplementation "org.junit.jupiter:junit-jupiter-api"
    testCompile "org.junit.jupiter:junit-jupiter-engine:5.1.0"
    testImplementation "io.micronaut.test:micronaut-test-junit5"
    testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine"
}

test.classpath += configurations.developmentOnly

mainClassName = "micronaut.test.Application"
// use JUnit 5 platform
test {
    useJUnitPlatform()
}
tasks.withType(JavaCompile){
    options.encoding = "UTF-8"
    options.compilerArgs.add('-parameters')
}

shadowJar {
    mergeServiceFiles()
}

run.classpath += configurations.developmentOnly
run.jvmArgs('-noverify', '-XX:TieredStopAtLevel=1', '-Dcom.sun.management.jmxremote')

存储库:

package micronaut.test.repo;

import io.micronaut.data.annotation.Repository;
import io.micronaut.data.repository.CrudRepository;
import micronaut.test.entity.Partner;

@Repository
public interface PartnerRepository extends CrudRepository<Partner,Integer> {
}

服务:

package micronaut.test.service;


import micronaut.test.entity.Partner;
import micronaut.test.repo.PartnerRepository;

import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.List;

@Singleton
public class SpannerService {
    private PartnerRepository partnerRepository;

    @Inject
    public SpannerService(PartnerRepository partnerRepository) {
        this.partnerRepository = partnerRepository;
    }

    public List<Partner> getPartners() {
        return (List<Partner>) partnerRepository.findAll();
    }
}

控制器:

package micronaut.test.controller;

import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.Produces;
import micronaut.test.entity.Partner;
import micronaut.test.service.SpannerService;

import javax.inject.Inject;
import java.util.List;

@Controller("/micronaut")
public class MainController {

    private SpannerService spannerService;

    @Inject
    public MainController(SpannerService spannerService) {
        this.spannerService = spannerService;
    }

    @Get("/data")
    @Produces(MediaType.APPLICATION_JSON)
    public List<Partner> getPartners() {
        return spannerService.getPartners();
    }

}

堆栈跟踪:

io.micronaut.context.exceptions.ConfigurationException: No backing RepositoryOperations configured for repository. Check your configuration and try again
        at io.micronaut.data.intercept.DataIntroductionAdvice.findInterceptor(DataIntroductionAdvice.java:108)
        at io.micronaut.data.intercept.DataIntroductionAdvice.intercept(DataIntroductionAdvice.java:76)
        at io.micronaut.aop.MethodInterceptor.intercept(MethodInterceptor.java:40)
        at io.micronaut.aop.chain.InterceptorChain.proceed(InterceptorChain.java:150)
        at micronaut.test.repo.PartnerRepository$Intercepted.findAll(Unknown Source)
        at micronaut.test.service.SpannerService.getPartners(SpannerService.java:22)
        at micronaut.test.controller.MainController.getPartners(MainController.java:32)
        at micronaut.test.controller.$MainControllerDefinition$$exec2.invokeInternal(Unknown Source)
        at io.micronaut.context.AbstractExecutableMethod.invoke(AbstractExecutableMethod.java:144)
        at io.micronaut.context.DefaultBeanContext$BeanExecutionHandle.invoke(DefaultBeanContext.java:2792)
        at io.micronaut.web.router.AbstractRouteMatch.execute(AbstractRouteMatch.java:235)
        at io.micronaut.web.router.RouteMatch.execute(RouteMatch.java:122)
        at io.micronaut.http.server.netty.RoutingInBoundHandler.lambda$buildResultEmitter$19(RoutingInBoundHandler.java:1408)
        at io.reactivex.internal.operators.flowable.FlowableCreate.subscribeActual(FlowableCreate.java:71)
        at io.reactivex.Flowable.subscribe(Flowable.java:14918)
        at io.reactivex.Flowable.subscribe(Flowable.java:14865)
        at io.micronaut.reactive.rxjava2.RxInstrumentedFlowable.subscribeActual(RxInstrumentedFlowable.java:68)
        at io.reactivex.Flowable.subscribe(Flowable.java:14918)
        at io.reactivex.internal.operators.flowable.FlowableMap.subscribeActual(FlowableMap.java:37)
        at io.reactivex.Flowable.subscribe(Flowable.java:14918)
        at io.reactivex.Flowable.subscribe(Flowable.java:14865)
        at io.micronaut.reactive.rxjava2.RxInstrumentedFlowable.subscribeActual(RxInstrumentedFlowable.java:68)
        at io.reactivex.Flowable.subscribe(Flowable.java:14918)
        at io.reactivex.internal.operators.flowable.FlowableSwitchIfEmpty.subscribeActual(FlowableSwitchIfEmpty.java:32)
        at io.reactivex.Flowable.subscribe(Flowable.java:14918)
        at io.reactivex.Flowable.subscribe(Flowable.java:14865)
        at io.micronaut.reactive.rxjava2.RxInstrumentedFlowable.subscribeActual(RxInstrumentedFlowable.java:68)
        at io.reactivex.Flowable.subscribe(Flowable.java:14918)
        at io.reactivex.Flowable.subscribe(Flowable.java:14868)
        at io.micronaut.http.context.ServerRequestTracingPublisher.lambda$subscribe$0(ServerRequestTracingPublisher.java:52)
        at io.micronaut.http.context.ServerRequestContext.with(ServerRequestContext.java:52)
        at io.micronaut.http.context.ServerRequestTracingPublisher.subscribe(ServerRequestTracingPublisher.java:52)
        at io.reactivex.internal.operators.flowable.FlowableFromPublisher.subscribeActual(FlowableFromPublisher.java:29)
        at io.reactivex.Flowable.subscribe(Flowable.java:14918)
        at io.reactivex.Flowable.subscribe(Flowable.java:14865)
        at io.micronaut.reactive.rxjava2.RxInstrumentedFlowable.subscribeActual(RxInstrumentedFlowable.java:68)
        at io.reactivex.Flowable.subscribe(Flowable.java:14918)
        at io.reactivex.Flowable.subscribe(Flowable.java:14865)
        at io.reactivex.internal.operators.flowable.FlowableSubscribeOn$SubscribeOnSubscriber.run(FlowableSubscribeOn.java:82)
        at io.reactivex.internal.schedulers.ExecutorScheduler$ExecutorWorker$BooleanRunnable.run(ExecutorScheduler.java:288)
        at io.reactivex.internal.schedulers.ExecutorScheduler$ExecutorWorker.run(ExecutorScheduler.java:253)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at java.lang.Thread.run(Thread.java:748)
Caused by: io.micronaut.context.exceptions.NoSuchBeanException: No bean of type [io.micronaut.data.operations.RepositoryOperations] exists. Make sure the bean is not disabled by bean requirements (enable trace logging for 'io.micronaut.context.condition' to check) and if the bean is enabled then ensure the class is declared a bean and annotation processing is enabled (for Java and Kotlin the 'micronaut-inject-java' dependency should be configured as an annotation processor).
        at io.micronaut.context.DefaultBeanContext.getBeanInternal(DefaultBeanContext.java:1903)
        at io.micronaut.context.DefaultBeanContext.getBean(DefaultBeanContext.java:582)
        at io.micronaut.data.intercept.DataIntroductionAdvice.findInterceptor(DataIntroductionAdvice.java:105)
        ... 43 common frames omitted

4

2 回答 2

1

Micronaut 目前仅支持开箱即用的 Tomcat JDBC、Apache DBCP2 和 Hikari 数据源提供程序(请参阅https://micronaut-projects.github.io/micronaut-sql/latest/guide/#jdbc)。

您可以将此行添加到您的build.gradle中,它将 Tomcat JDBC 数据源提供程序实现添加到您的项目中:

runtime "io.micronaut.configuration:micronaut-jdbc-tomcat"

或者您可以选择其他实现,例如 Apache DBCP2:

runtime "io.micronaut.configuration:micronaut-jdbc-dbcp"

或光:

runtime "io.micronaut.configuration:micronaut-jdbc-hikari"

对于nl.topicus:spanner-jdbc数据源提供者,您必须自己实现DatasourceFactoryDatasourceConfiguration对于 Micronaut,因为还没有。

您可以在io.micronaut.configuration:micronaut-jdbc-tomcat. 来源在这里:https ://github.com/micronaut-projects/micronaut-sql/tree/master/jdbc-tomcat/src/main/java/io/micronaut/configuration/jdbc/tomcat

例如DatasourceFactory可以看起来像这样:

@Factory
public class DatasourceFactory implements AutoCloseable {
    private static final Logger LOG = LoggerFactory.getLogger(DatasourceFactory.class);
    private List<nl.topicus.jdbc.CloudSpannerDataSource> dataSources = new ArrayList<>(2);

    private final DataSourceResolver dataSourceResolver;

    /**
     * Default constructor.
     * @param dataSourceResolver The data source resolver
     */
    public DatasourceFactory(@Nullable DataSourceResolver dataSourceResolver) {
        this.dataSourceResolver = dataSourceResolver == null ? DataSourceResolver.DEFAULT : dataSourceResolver;
    }

    /**
     * @param datasourceConfiguration A {@link DatasourceConfiguration}
     * @return An Apache Tomcat {@link DataSource}
     */
    @Context
    @EachBean(DatasourceConfiguration.class)
    public DataSource dataSource(DatasourceConfiguration datasourceConfiguration) {
        nl.topicus.jdbc.CloudSpannerDataSource ds = new nl.topicus.jdbc.CloudSpannerDataSource();
        ds.setJdbcUrl(datasourceConfiguration.getJdbcUrl());
        ...
        dataSources.add(ds);
        return ds;
    }

    @Override
    @PreDestroy
    public void close() {
        for (nl.topicus.jdbc.CloudSpannerDataSource dataSource : dataSources) {
            try {
                dataSource.close();
            } catch (Exception e) {
                if (LOG.isWarnEnabled()) {
                    LOG.warn("Error closing data source [" + dataSource + "]: " + e.getMessage(), e);
                }
            }
        }
    }
}

无论如何,您仍然可以将 Google Cloud Spanner DB 与 Hikari 和 Apache DBCP2 等数据源提供程序一起使用。例如:

runtime 'nl.topicus:spanner-jdbc:1.1.5'
runtime "io.micronaut.configuration:micronaut-jdbc-hikari"

第一行添加 JDBC 驱动程序,第二行添加将使用spanner-jdbcJDBC 驱动程序的数据源提供程序。

于 2019-09-09T07:16:57.953 回答
0

众所周知,Google Cloud Spanner 为 Hibernate ORM 使用了不同于其他数据库的另一种方言。我认为这种方言很新。看看这个存储库1。也许它对你有用,不是解决你当前的问题所必需的,而是给你一些其他的观点。

于 2019-09-09T10:22:02.870 回答