无法初始化 JPA EntityManagerFactory:无法创建请求的服务 [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
Posted
技术标签:
【中文标题】无法初始化 JPA EntityManagerFactory:无法创建请求的服务 [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]【英文标题】:Failed to initialize JPA EntityManagerFactory: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment] 【发布时间】:2021-12-22 00:27:20 【问题描述】:我的应用程序在本地环境中正常运行。但在 Docker(AWS EC2)中不起作用。
当我docker logs my-spring-image
2021-11-09 11:10:49.793 ERROR 7 --- [ main] o.s.boot.SpringApplication : Application run failed
java.lang.NoClassDefFoundError: org/locationtech/jts/geom/Geometry
at java.base/java.lang.Class.getDeclaredMethods0(Native Method) ~[na:na]
at java.base/java.lang.Class.privateGetDeclaredMethods(Class.java:3166) ~[na:na]
at java.base/java.lang.Class.privateGetPublicMethods(Class.java:3191) ~[na:na]
at java.base/java.lang.Class.getMethods(Class.java:1904) ~[na:na]
at org.springframework.data.repository.util.ReactiveWrappers.usesReactiveType(ReactiveWrappers.java:159) ~[spring-data-commons-2.5.5.jar!/:2.5.5]
at org.springframework.data.repository.core.support.AbstractRepositoryMetadata.isReactiveRepository(AbstractRepositoryMetadata.java:160) ~[spring-data-commons-2.5.5.jar!/:2.5.5]
at org.springframework.data.repository.config.RepositoryConfigurationExtensionSupport.useRepositoryConfiguration(RepositoryConfigurationExtensionSupport.java:366) ~[spring-data-commons-2.5.5.jar!/:2.5.5]
这是我的 build.gradle
buildscript
ext
queryDslVersion = "4.4.0"
plugins
id 'org.springframework.boot' version '2.5.5'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
// querydsl
id "com.ewerk.gradle.plugins.querydsl" version "1.0.10"
group = 'ssafy'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
configurations
compileOnly
extendsFrom annotationProcessor
repositories
mavenCentral()
dependencies
// Web
implementation 'org.springframework.boot:spring-boot-starter-web'
// WebFlux
implementation 'org.springframework.boot:spring-boot-starter-webflux'
// mysql Database
runtimeOnly 'mysql:mysql-connector-java'
//spatial query
compileOnly 'org.hibernate:hibernate-spatial'
// JPA Hibernate
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
// Lombok
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testCompileOnly 'org.projectlombok:lombok'
testAnnotationProcessor 'org.projectlombok:lombok'
// Spring Security
implementation 'org.springframework.boot:spring-boot-starter-security'
// java-jwt : https://github.com/auth0/java-jwt
implementation 'com.auth0:java-jwt:3.18.2'
// OAuth
implementation 'org.springframework.boot:spring-boot-starter-oauth2-client'
// Validation
implementation 'org.springframework.boot:spring-boot-starter-validation'
//firebase
implementation 'com.google.firebase:firebase-admin:6.8.1'
// okhttp
implementation group: 'com.squareup.okhttp3', name: 'okhttp', version: '4.2.2'
// swagger
implementation 'io.springfox:springfox-swagger-ui:3.0.0'
implementation 'io.springfox:springfox-boot-starter:3.0.0'
// Dev Tools
developmentOnly 'org.springframework.boot:spring-boot-devtools'
// p6spy : https://github.com/gavlyukovskiy/spring-boot-data-source-decorator
implementation 'com.github.gavlyukovskiy:p6spy-spring-boot-starter:1.7.1'
// Test
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
// JsonObject 사용
implementation group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1.1'
//Tymeleaf
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect'
// s3
implementation('org.springframework.cloud:spring-cloud-starter-aws:2.2.6.RELEASE')
//javax
implementation("javax.annotation:javax.annotation-api:1.3.2")
annotationProcessor("javax.annotation:javax.annotation-api:1.3.2")
// querydsl
implementation 'com.querydsl:querydsl-jpa'
test
useJUnitPlatform()
// querydsl
def querydslDir = "$buildDir/generated/querydsl"
querydsl
jpa = true
querydslSourcesDir = querydslDir
sourceSets
main.java.srcDir querydslDir
configurations
querydsl.extendsFrom compileClasspath
compileQuerydsl
options.annotationProcessorPath = configurations.querydsl
这是 application.yml
spring:
profiles:
include: API-KEY
## MYSQL 일 경우에
datasource:
url: jdbc:mysql://ip-address:3306/coffee_station?useSSL=false&allowPublicKeyRetrieval=true&characterEncoding=UTF-8&serverTimezone=UTC
username: username
password: password
driver-class-name: com.mysql.cj.jdbc.Driver
jpa:
database: mysql
database-platform: org.hibernate.spatial.dialect.mysql.MySQL56InnoDBSpatialDialect
hibernate:
ddl-auto: update
properties:
hibernate:
format_sql: true
default_batch_fetch_size: 500
open-in-view: false
logging.level:
org.hibernate.SQL: debug
decorator:
datasource:
p6spy:
enable-logging: true
jwt:
secret: ---------secret key----------
cloud:
aws:
s3:
bucket: bucket-name
region:
static: ap-northeast-2
stack:
auto: false
这是我的 dockerfile
FROM openjdk:11-jdk AS build
WORKDIR /app
COPY gradlew .
COPY gradle gradle
COPY build.gradle .
COPY settings.gradle .
COPY src src
COPY src/main/resources/application.yml src/main/resources/application.yml
COPY src/main/resources/application-API-KEY.properties src/main/resources/application-API-KEY.properties
RUN cat src/main/resources/application.yml
RUN cat src/main/resources/application-API-KEY.properties
RUN chmod +x gradlew
RUN rm -rf build/generated
RUN ["./gradlew", "clean", "build", "--stacktrace", "--exclude-task", "test"]
FROM openjdk:11-jdk
COPY --from=build /app/build/libs/runner-0.0.1-SNAPSHOT.jar app.jar
EXPOSE 8080
ENTRYPOINT java -jar app.jar
詹金斯总是说“建立成功”
我在 APP/resources 下也检查了 application.yml 和 application-API-KEY.properties 创建成功。
请帮助我... :) 谢谢
【问题讨论】:
【参考方案1】:我在 build.gradle 中发现了我的错误。
这个问题是由“compileOnly”引起的
我更改了我的build.gradle
。
compileOnly 'org.hibernate:hibernate-spatial'
->implementation 'org.hibernate:hibernate-spatial'
然后它就正常工作了。
【讨论】:
以上是关于无法初始化 JPA EntityManagerFactory:无法创建请求的服务 [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]的主要内容,如果未能解决你的问题,请参考以下文章
JPA LazyInitialisationException - 无法初始化代理
Docker中的SpringBoot + Postgres:在docker-compose上:无法初始化JPA EntityManagerFactory:无法加载类
无法初始化 JPA EntityManagerFactory:无法创建请求的服务 [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
springboot jpa oracle实体类中配置注解无法在数据库中生成注释的一种解决方式