[spring-projects/spring-boot]Spring Boot 3 编译为本机时出现 Postgresql JDBC 错误

2024-06-26 609 views
7

我有一个简单的 Spring Boot 3 应用程序,它使用 Spring Data JPA(https://github.com/edeandrea/quarkus-for-spring-devs-examples/tree/quarkus-for-spring-devs-talk-sb3/chapter-4/chapter-4-spring-data-jpa)。

该应用程序的配置(https://github.com/edeandrea/quarkus-for-spring-devs-examples/blob/quarkus-for-spring-devs-talk-sb3/chapter-4/chapter-4-spring-data-jpa/src/main/resources/application.yml)有spring.jpa.hibernate.ddl-auto=create-drop,并且有一个import.sql文件src/main/resources

GraalVM CE 22.3.0 (build 17.0.5+8-jvmci-22.3-b08)使用命令编译为本机( )./mvnw -Pnative native:compile并运行应用程序后,它启动了,但数据import.sql未加载。相反,我看到以下错误。

注意:这在 3.0.0-RC1 中运行良好,但在 3.0.0-RC2 中出现同样的错误。

2022-11-28T10:14:33.953-05:00  WARN 62921 --- [           main] o.h.dialect.PostgreSQLPGObjectJdbcType   : PostgreSQL JDBC driver classes are inaccessible and thus, certain DDL types like JSONB, JSON, GEOMETRY can not be used!

java.lang.NoSuchMethodException: org.postgresql.util.PGobject.<init>()
        at java.base@17.0.5/java.lang.Class.getConstructor0(DynamicHub.java:3585) ~[chapter-4-spring-data-jpa:na]
        at java.base@17.0.5/java.lang.Class.getConstructor(DynamicHub.java:2271) ~[chapter-4-spring-data-jpa:na]
        at org.hibernate.dialect.PostgreSQLPGObjectJdbcType.<clinit>(PostgreSQLPGObjectJdbcType.java:50) ~[chapter-4-spring-data-jpa:6.1.5.Final]
        at org.hibernate.dialect.PostgreSQLDialect.registerColumnTypes(PostgreSQLDialect.java:231) ~[chapter-4-spring-data-jpa:6.1.5.Final]
        at org.hibernate.dialect.Dialect.contributeTypes(Dialect.java:1341) ~[chapter-4-spring-data-jpa:6.1.5.Final]
        at org.hibernate.dialect.PostgreSQLDialect.contributeTypes(PostgreSQLDialect.java:1229) ~[chapter-4-spring-data-jpa:6.1.5.Final]
        at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.handleTypes(MetadataBuildingProcess.java:386) ~[na:na]
        at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:143) ~[na:na]
        at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:1350) ~[chapter-4-spring-data-jpa:6.1.5.Final]
        at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1421) ~[chapter-4-spring-data-jpa:6.1.5.Final]
        at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:66) ~[na:na]
        at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:376) ~[chapter-4-spring-data-jpa:6.0.2]
        at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:409) ~[chapter-4-spring-data-jpa:6.0.2]
        at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:396) ~[chapter-4-spring-data-jpa:6.0.2]
        at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:352) ~[chapter-4-spring-data-jpa:6.0.2]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1797) ~[chapter-4-spring-data-jpa:6.0.2]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1747) ~[chapter-4-spring-data-jpa:6.0.2]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:599) ~[chapter-4-spring-data-jpa:6.0.2]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:521) ~[chapter-4-spring-data-jpa:6.0.2]
        at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:326) ~[chapter-4-spring-data-jpa:6.0.2]
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[chapter-4-spring-data-jpa:6.0.2]
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324) ~[chapter-4-spring-data-jpa:6.0.2]
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[chapter-4-spring-data-jpa:6.0.2]
        at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1130) ~[chapter-4-spring-data-jpa:6.0.2]
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:905) ~[chapter-4-spring-data-jpa:6.0.2]
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:584) ~[chapter-4-spring-data-jpa:6.0.2]
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146) ~[chapter-4-spring-data-jpa:3.0.0]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:730) ~[chapter-4-spring-data-jpa:3.0.0]
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:432) ~[chapter-4-spring-data-jpa:3.0.0]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:308) ~[chapter-4-spring-data-jpa:3.0.0]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1302) ~[chapter-4-spring-data-jpa:3.0.0]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1291) ~[chapter-4-spring-data-jpa:3.0.0]
        at org.acme.Chapter4SpringDataJpaApplication.main(Chapter4SpringDataJpaApplication.java:10) ~[chapter-4-spring-data-jpa:na]

以下是完整日志:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v3.0.0)

2022-11-28T10:14:33.852-05:00  INFO 62921 --- [           main] o.acme.Chapter4SpringDataJpaApplication  : Starting AOT-processed Chapter4SpringDataJpaApplication using Java 17.0.5 with PID 62921 (/Users/edeandre/workspaces/quarkus-ebook/quarkus-for-spring-devs-examples/chapter-4/chapter-4-spring-data-jpa/target/chapter-4-spring-data-jpa started by edeandre in /Users/edeandre/workspaces/quarkus-ebook/quarkus-for-spring-devs-examples/chapter-4/chapter-4-spring-data-jpa)
2022-11-28T10:14:33.853-05:00  INFO 62921 --- [           main] o.acme.Chapter4SpringDataJpaApplication  : No active profile set, falling back to 1 default profile: "default"
2022-11-28T10:14:33.877-05:00  INFO 62921 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2022-11-28T10:14:33.878-05:00  INFO 62921 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2022-11-28T10:14:33.878-05:00  INFO 62921 --- [           main] o.apache.catalina.core.StandardEngine    : Starting Servlet engine: [Apache Tomcat/10.1.1]
2022-11-28T10:14:33.882-05:00  INFO 62921 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2022-11-28T10:14:33.882-05:00  INFO 62921 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 29 ms
2022-11-28T10:14:33.900-05:00  INFO 62921 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2022-11-28T10:14:33.901-05:00  INFO 62921 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 6.1.5.Final
2022-11-28T10:14:33.903-05:00  WARN 62921 --- [           main] org.hibernate.orm.deprecation            : HHH90000021: Encountered deprecated setting [javax.persistence.sharedCache.mode], use [jakarta.persistence.sharedCache.mode] instead
2022-11-28T10:14:33.905-05:00  INFO 62921 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2022-11-28T10:14:33.948-05:00  INFO 62921 --- [           main] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Added connection org.postgresql.jdbc.PgConnection@726fe6b2
2022-11-28T10:14:33.948-05:00  INFO 62921 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2022-11-28T10:14:33.952-05:00  INFO 62921 --- [           main] SQL dialect                              : HHH000400: Using dialect: org.hibernate.dialect.PostgreSQLDialect
2022-11-28T10:14:33.953-05:00  WARN 62921 --- [           main] o.h.dialect.PostgreSQLPGObjectJdbcType   : PostgreSQL JDBC driver classes are inaccessible and thus, certain DDL types like JSONB, JSON, GEOMETRY can not be used!

java.lang.NoSuchMethodException: org.postgresql.util.PGobject.<init>()
        at java.base@17.0.5/java.lang.Class.getConstructor0(DynamicHub.java:3585) ~[chapter-4-spring-data-jpa:na]
        at java.base@17.0.5/java.lang.Class.getConstructor(DynamicHub.java:2271) ~[chapter-4-spring-data-jpa:na]
        at org.hibernate.dialect.PostgreSQLPGObjectJdbcType.<clinit>(PostgreSQLPGObjectJdbcType.java:50) ~[chapter-4-spring-data-jpa:6.1.5.Final]
        at org.hibernate.dialect.PostgreSQLDialect.registerColumnTypes(PostgreSQLDialect.java:231) ~[chapter-4-spring-data-jpa:6.1.5.Final]
        at org.hibernate.dialect.Dialect.contributeTypes(Dialect.java:1341) ~[chapter-4-spring-data-jpa:6.1.5.Final]
        at org.hibernate.dialect.PostgreSQLDialect.contributeTypes(PostgreSQLDialect.java:1229) ~[chapter-4-spring-data-jpa:6.1.5.Final]
        at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.handleTypes(MetadataBuildingProcess.java:386) ~[na:na]
        at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:143) ~[na:na]
        at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:1350) ~[chapter-4-spring-data-jpa:6.1.5.Final]
        at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1421) ~[chapter-4-spring-data-jpa:6.1.5.Final]
        at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:66) ~[na:na]
        at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:376) ~[chapter-4-spring-data-jpa:6.0.2]
        at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:409) ~[chapter-4-spring-data-jpa:6.0.2]
        at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:396) ~[chapter-4-spring-data-jpa:6.0.2]
        at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:352) ~[chapter-4-spring-data-jpa:6.0.2]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1797) ~[chapter-4-spring-data-jpa:6.0.2]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1747) ~[chapter-4-spring-data-jpa:6.0.2]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:599) ~[chapter-4-spring-data-jpa:6.0.2]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:521) ~[chapter-4-spring-data-jpa:6.0.2]
        at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:326) ~[chapter-4-spring-data-jpa:6.0.2]
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[chapter-4-spring-data-jpa:6.0.2]
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324) ~[chapter-4-spring-data-jpa:6.0.2]
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[chapter-4-spring-data-jpa:6.0.2]
        at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1130) ~[chapter-4-spring-data-jpa:6.0.2]
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:905) ~[chapter-4-spring-data-jpa:6.0.2]
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:584) ~[chapter-4-spring-data-jpa:6.0.2]
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146) ~[chapter-4-spring-data-jpa:3.0.0]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:730) ~[chapter-4-spring-data-jpa:3.0.0]
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:432) ~[chapter-4-spring-data-jpa:3.0.0]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:308) ~[chapter-4-spring-data-jpa:3.0.0]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1302) ~[chapter-4-spring-data-jpa:3.0.0]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1291) ~[chapter-4-spring-data-jpa:3.0.0]
        at org.acme.Chapter4SpringDataJpaApplication.main(Chapter4SpringDataJpaApplication.java:10) ~[chapter-4-spring-data-jpa:na]

2022-11-28T10:14:34.045-05:00  WARN 62921 --- [           main] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Warning Code: 0, SQLState: 00000
2022-11-28T10:14:34.045-05:00  WARN 62921 --- [           main] o.h.engine.jdbc.spi.SqlExceptionHelper   : table "fruits" does not exist, skipping
2022-11-28T10:14:34.072-05:00  INFO 62921 --- [           main] o.h.e.t.j.p.i.JtaPlatformInitiator       : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2022-11-28T10:14:34.073-05:00  INFO 62921 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2022-11-28T10:14:34.101-05:00  WARN 62921 --- [           main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2022-11-28T10:14:34.117-05:00  WARN 62921 --- [           main] i.m.c.i.binder.jvm.JvmGcMetrics          : GC notifications will not be available because MemoryPoolMXBeans are not provided by the JVM
2022-11-28T10:14:34.120-05:00  INFO 62921 --- [           main] o.s.b.a.e.web.EndpointLinksResolver      : Exposing 1 endpoint(s) beneath base path '/actuator'
2022-11-28T10:14:34.122-05:00  INFO 62921 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2022-11-28T10:14:34.123-05:00  INFO 62921 --- [           main] o.acme.Chapter4SpringDataJpaApplication  : Started Chapter4SpringDataJpaApplication in 0.288 seconds (process running for 0.304)

回答

4

对我来说,这看起来像是一个警告而不是错误,这是由于 Hibernate 的 Postgres 支持中的一些反射未被其可达性元数据所涵盖。我打开了https://github.com/oracle/graalvm-reachability-metadata/issues/131

import.sql对于 Spring Boot 应用来说,使用是不常见的。我们通常建议使用schema.sqldata.sql文件,它们将自动包含在本机映像中并由 Spring Boot 处理。为了让import.sqlHibernate 找到并处理 ,我认为您必须手动配置 Graal 以将其包含在内。你这样做了吗?

3

我还没有这样做,但会尝试将其重命名data.sql,看看会发生什么。我已经import.sql在这个应用程序中使用了一年,通过 Spring Boot 2.6.x 和 2.7.x 升级,以及 3.0.0-RC1。直到 3.0.0-RC2 才出现故障。

我会汇报我的发现。感谢您如此迅速地调查此事!

8

因此更改import.sqldata.sql以及添加

spring.jpa.defer-datasource-initialization=true
spring.sql.init.mode=always

修复了运行时问题,但它破坏了我的单元测试(https://github.com/edeandrea/quarkus-for-spring-devs-examples/blob/quarkus-for-spring-devs-talk-sb3/chapter-4/chapter-4-spring-data-jpa/src/test/java/org/acme/repository/FruitRepositoryTests.java)。

测试运行时似乎data.sql没有运行。测试希望找到数据库中已有的数据,但在进行此更改后,这些数据就不存在了。

在测试中一切运行import.sql正常,但是在运行时却出现问题。

5

出现这种情况的原因有多种,但您提供的信息不足以让我们判断是哪一种原因。我怀疑application.yml您在类路径上拥有的重复文件可能是问题的一部分,但这只是猜测,因为您没有详细描述您所做的更改,因此我们无法确定。

如果你还有其他问题,请关注 Stack Overflow 或Gitter 。正如贡献指南中所述,我们更喜欢仅将 GitHub 问题用于错误和增强功能。

6

感谢@wilkinsona的指点。添加

spring.jpa.defer-datasource-initialization=true
spring.sql.init.mode=always

进入application.yml修复src/test/resources测试。

3

我也遇到了这个问题,并通过执行临时修复了它RuntimeHintsRegistrar

@Configuration
@ImportRuntimeHints(NativeImageRuntimeHintsConfiguration.HibernateRegistrar.class)
public class NativeImageRuntimeHintsConfiguration {

    static class HibernateRegistrar implements RuntimeHintsRegistrar {
        @Override
        public void registerHints(final RuntimeHints hints, final ClassLoader classLoader) {
            try {
                // Temporary hint, should be included into the official spring boot project
                hints.reflection().registerTypeIfPresent(classLoader, "org.postgresql.util.PGobject",
                    (hint) -> hint.withMembers(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS, MemberCategory.INTROSPECT_PUBLIC_METHODS)
                        .onReachableType(PostgreSQLPGObjectJdbcType.class));
            } catch (final Exception e) {
                throw new RuntimeException(e);
            }
        }
    }

}
4

感谢@NicklasWallgren。我可以通过添加以下内容来修复它

spring:
  jpa:
    defer-datasource-initialization: true
  sql:
    init:
      mode: always

进入/src/test/resources/application.yml

9

另一个解决方法是添加META-INF/native-image/reflect-config.json以下内容:

[
    {
        "name": "org.postgresql.util.PGobject",
        "allDeclaredConstructors": true
    }
]
5

谢谢@philwebb。即使在 Boot 3.0.1 上,java.lang.NoSuchMethodException: org.postgresql.util.PGobject.<init>()即使没有添加该警告,我仍然会看到该警告。不过这似乎对功能没有影响。

2

未来是否有计划允许使用除 之外的数据文件构建本机图像data.sql?在非本机 Spring Boot 中,我们可以设置属性sql.datasource.data- 但经过一些测试后,这似乎不适用于本机 spring 图像(除非它特定于我构建的方式)。

8

@ft9054 只要您告诉 Graal 将非标准数据文件作为资源包含在本机映像中,这应该已经起作用了。

6

data.sql@wilkinsona 感谢您的快速回复!我尝试通过该属性更新应用程序的 application.yml 以使用其他文件sql.datasource.data。有没有 Graal 特定的方法来做到这一点?您指的是像这里这样的资源吗?