graalvm-reachability-metadata icon indicating copy to clipboard operation
graalvm-reachability-metadata copied to clipboard

Hibernate spatial failing when running in native

Open neogeogre opened this issue 2 years ago • 1 comments

Describe the bug

Hibernate spatial is failing when running in native image.

To Reproduce

Create a spring boot 3.0.2 project using hibernate-spatial in build.gradle.kts:

implementation("org.hibernate.orm:hibernate-spatial:6.1.7.Final")

application.yaml:

spring:
  jpa:
    database-platform: org.hibernate.dialect.PostgreSQLDialect
    database: postgresql

The native image generation is working with ./gradlew clean nativeCompile, but then running the executable will generate the error log bellow.

Expected behavior

Expected behavior is to have the spring boot app starting properly, like it is already the case when using the JVM with ./gradlew bootRun, the error does not appear.

Logs

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

2023-02-15T15:43:23.174+01:00  INFO 31733 --- [           main] io.myApp.AppKt                : Starting AOT-processed AppKt using Java 17.0.5 with PID 31733 (/home/geoffrey/Desktop/gitlab/geozone-service2/build/native/nativeCompile/geozone-service2 started by geoffrey in /home/geoffrey/Desktop/gitlab/geozone-service2)
2023-02-15T15:43:23.174+01:00  INFO 31733 --- [           main] io.myApp.AppKt                : No active profile set, falling back to 1 default profile: "default"
2023-02-15T15:43:23.204+01:00  INFO 31733 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2023-02-15T15:43:23.204+01:00  INFO 31733 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2023-02-15T15:43:23.204+01:00  INFO 31733 --- [           main] o.apache.catalina.core.StandardEngine    : Starting Servlet engine: [Apache Tomcat/10.1.5]
2023-02-15T15:43:23.208+01:00  INFO 31733 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2023-02-15T15:43:23.208+01:00  INFO 31733 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 34 ms
2023-02-15T15:43:23.231+01:00  INFO 31733 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2023-02-15T15:43:23.232+01:00  INFO 31733 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 6.1.6.Final
2023-02-15T15:43:23.232+01:00  WARN 31733 --- [           main] w.s.c.ServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory': null
2023-02-15T15:43:23.232+01:00  INFO 31733 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2023-02-15T15:43:23.233+01:00 ERROR 31733 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory': null
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1751) ~[geozone-service2:6.0.4]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:599) ~[geozone-service2:6.0.4]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:521) ~[geozone-service2:6.0.4]
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:326) ~[geozone-service2:6.0.4]
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[geozone-service2:6.0.4]
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324) ~[geozone-service2:6.0.4]
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[geozone-service2:6.0.4]
	at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1130) ~[geozone-service2:6.0.4]
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:905) ~[geozone-service2:6.0.4]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:584) ~[geozone-service2:6.0.4]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146) ~[geozone-service2:3.0.2]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:730) ~[geozone-service2:3.0.2]
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:432) ~[geozone-service2:3.0.2]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:308) ~[geozone-service2:3.0.2]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1302) ~[geozone-service2:3.0.2]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1291) ~[geozone-service2:3.0.2]
	at io.myApp.AppKt.main(App.kt:19) ~[geozone-service2:na]
Caused by: java.lang.ExceptionInInitializerError: null
	at org.hibernate.spatial.integration.SpatialInitializer.contribute(SpatialInitializer.java:22) ~[geozone-service2:6.1.7.Final]
	at org.hibernate.boot.registry.StandardServiceRegistryBuilder.applyServiceContributors(StandardServiceRegistryBuilder.java:383) ~[geozone-service2:6.1.6.Final]
	at org.hibernate.boot.registry.StandardServiceRegistryBuilder.build(StandardServiceRegistryBuilder.java:362) ~[geozone-service2:6.1.6.Final]
	at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.<init>(EntityManagerFactoryBuilderImpl.java:265) ~[geozone-service2:6.1.6.Final]
	at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.<init>(EntityManagerFactoryBuilderImpl.java:189) ~[geozone-service2:6.1.6.Final]
	at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60) ~[na:na]
	at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:376) ~[geozone-service2:na]
	at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:409) ~[geozone-service2:na]
	at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:396) ~[geozone-service2:na]
	at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:352) ~[geozone-service2:na]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1797) ~[geozone-service2:6.0.4]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1747) ~[geozone-service2:6.0.4]
	... 16 common frames omitted
Caused by: java.lang.IllegalArgumentException: Invalid logger interface org.hibernate.spatial.HSMessageLogger (implementation not found in jdk.internal.loader.ClassLoaders$AppClassLoader@f5f2bb7)
	at org.jboss.logging.Logger.doGetMessageLogger(Logger.java:2564) ~[geozone-service2:3.5.0.Final]
	at org.jboss.logging.Logger.getMessageLogger(Logger.java:2530) ~[geozone-service2:3.5.0.Final]
	at org.jboss.logging.Logger.getMessageLogger(Logger.java:2516) ~[geozone-service2:3.5.0.Final]
	at org.hibernate.spatial.integration.SpatialService.<clinit>(SpatialService.java:29) ~[na:na]
	... 28 common frames omitted

System Info (please complete the following information):

  • OS: ubuntu 22.04
  • GraalVM Version 22.3.0
  • Java Version Java 17 CE
  • Plugin version 0.9.18

neogeogre avatar Feb 15 '23 16:02 neogeogre

@neogeogre Looks like missing logger reflection entries, similar to what I did in https://github.com/oracle/graalvm-reachability-metadata/pull/298 for Hibernate ORM 6.2. Could you maybe try to craft a PR to add missing reflection entries for *_$logger (including the org.hibernate.spatial.HSMessageLogger one from the error message) in Hibernate Spatial like I did in my PR (with tests if possible depending on class/method visibility)?

sdeleuze avatar May 24 '23 13:05 sdeleuze

Hey team! Is there any updates on this? I'm having the same issue. Can we temporarily fix this with the RuntimeHintsRegistrar?

RMCampos avatar May 14 '24 18:05 RMCampos

Should be fixed by #502.

As a workaround, you can use with Spring:

@SpringBootApplication
@ImportRuntimeHints(DemoApplication.Hints.class)
public class DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}

	static class Hints implements RuntimeHintsRegistrar {

		@Override
		public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
			hints.reflection().registerType(TypeReference.of("org.hibernate.spatial.HSMessageLogger_$logger"),
					typeHint -> typeHint.withConstructor(List.of(TypeReference.of("org.jboss.logging.Logger")), ExecutableMode.INVOKE));
		}
	}

}

sdeleuze avatar Jun 06 '24 12:06 sdeleuze