使用 micronaut-data-hibernate-jpa 库连接到 jpa 时出错

Posted

技术标签:

【中文标题】使用 micronaut-data-hibernate-jpa 库连接到 jpa 时出错【英文标题】:Getting error while connecting to jpa using micronaut-data-hibernate-jpa library 【发布时间】:2020-01-10 22:00:14 【问题描述】:

我想为 micronaut 使用 JPA。为此,我正在使用 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"
 

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

build.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

【问题讨论】:

【参考方案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 数据源提供者,您必须为 Micronaut 实现自己的 DatasourceFactoryDatasourceConfiguration,因为目前还没有。

您可以在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-jdbc JDBC 驱动程序的数据源提供程序。

【讨论】:

我已经有了这个:implementation 'nl.topicus:spanner-jdbc:1.1.5',这是 cloud-spanner 的第三方 jdbc 驱动程序 我用其他信息更新了答案。很快,您需要以 Micronaut 方式实现的 DataSource 提供程序。例如,您可以将 nl.topicus:spanner-jdbc:1.1.5 JDBC 驱动程序与 Hikari 一起使用,或者为 Micronaut 实现我们自己的 DatasourceFactory,它将直接从 nl.topicus:spanner-jdbc:1.1.5 library 使用 CloudSpannerDataSource 更不用说,如果你使用的是mysql,请在maven的gradle中包含mysql连接器的依赖【参考方案2】:

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

【讨论】:

嘿。谢谢您的帮助。好像方言还在开发中。我找不到要使用的方言版本,这会给我带来编译错误。知道我能不能找到方言版本吗? 我已经在上面的帖子中发布了这个存储库链接:github.com/GoogleCloudPlatform/google-cloud-spanner-hibernate。据我所知,这是迄今为止唯一为 Cloud Spanner 开发的东西。

以上是关于使用 micronaut-data-hibernate-jpa 库连接到 jpa 时出错的主要内容,如果未能解决你的问题,请参考以下文章

在使用加载数据流步骤的猪中,使用(使用 PigStorage)和不使用它有啥区别?

今目标使用教程 今目标任务使用篇

Qt静态编译时使用OpenSSL有三种方式(不使用,动态使用,静态使用,默认是动态使用)

MySQL db 在按日期排序时使用“使用位置;使用临时;使用文件排序”

使用“使用严格”作为“使用强”的备份

Kettle java脚本组件的使用说明(简单使用升级使用)