micronaut-projects / micronaut-data

Ahead of Time Data Repositories
Apache License 2.0
462 stars 195 forks source link

Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment] due to: Cannot invoke "String.length()" because "path" is null #3028

Open kareemelzayat opened 1 month ago

kareemelzayat commented 1 month ago

Expected Behavior

I initiated a micronaut project with Kotlin on Java 21 and the following features:

I am expecting that the generated test file to work out of the box and the test passes. The test file looks like this:

@MicronautTest
class DemoTest {

    @Inject
    lateinit var application: EmbeddedApplication<*>

    @Test
    fun testItWorks() {
        Assertions.assertTrue(application.isRunning)
    }

}

Actual Behaviour

Instead, I am getting the following error:

Bean definition [org.hibernate.SessionFactory] could not be loaded: Error instantiating bean of type  [org.hibernate.boot.SessionFactoryBuilder]

Message: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment] due to: Cannot invoke "String.length()" because "path" is null
Path Taken: SessionFactoryPerJpaConfigurationFactory.buildHibernateSessionFactoryBuilder(SessionFactoryBuilder sessionFactoryBuilder) --> SessionFactoryPerJpaConfigurationFactory.buildHibernateSessionFactoryBuilder([SessionFactoryBuilder sessionFactoryBuilder]) --> SessionFactoryPerJpaConfigurationFactory.buildHibernateSessionFactoryBuilder([Metadata metadata],JpaConfiguration jpaConfiguration)
io.micronaut.context.exceptions.BeanInstantiationException: Bean definition [org.hibernate.SessionFactory] could not be loaded: Error instantiating bean of type  [org.hibernate.boot.SessionFactoryBuilder]

Looking at the console a bit deeper, it seems that the SQL client URL is not properly calculated:

10:57:13.974 [Test worker] INFO  o.h.r.pool.impl.DefaultSqlClientPool - HR000011: SQL Client URL [jdbc:tc:postgresql:16.3:///db]

Notice the literal value jdbc:tc:postgresql:16.3:///db, which should instead look something like this jdbc:postgresql://localhost:55958/test?loggerLevel=OFF (the generated URL of the testcontainer)

Steps To Reproduce

1- Start a Micronaut Projec with Kotlin on Java 21 and Gradle

2- Install the dependencies:

3- Adjust the config files:

datasources { default { db-type = "postgres" dialect = "POSTGRES" driverClassName = "org.postgresql.Driver" password = "${DATABASE_PW:password}" url = "jdbc:postgresql://${DATABASE_URL:localhost:5432}/mydb" username = "${DATABASE_USER:sa}" } }

jpa { default { reactive = true entity-scan { packages = "com.mstqr.domain" } properties { hibernate { connection { db-type = "${datasources.default.db-type}" password = "${datasources.default.password}" url = "${datasources.default.url}" username = "${datasources.default.user}" } show_sql = true hbm2ddl { auto = "none" } } } } }


- `application-test.conf`:

datasources { default { driverClassName = "org.testcontainers.jdbc.ContainerDatabaseDriver" url = "jdbc:tc:postgresql:16.3:///db" } }

jpa { default { reactive = true properties { hibernate { connection { driverClassName = ${datasources.default.driverClassName} url = ${datasources.default.url} } hbm2ddl { auto = "none" } } } } }



### 4- Run any test via `./gradlew test`

### 5- Check the console for the error

### Environment Information

JDK version: 21
Kotlin version: 1.9.24
Build Tool: Gradle Kotlin
Test framework: Junit5

### Example Application

https://github.com/kareemelzayat/demo-micronaut

### Version

4.5.0
kareemelzayat commented 1 month ago

@dstepanov it seems there is an issue integrating Micronaut Data Hibernate Reactive with Testcontainers and Liquibase.

I tried everything I could to make it work, the test always fails.

dstepanov commented 1 month ago

Please create a sample app

kareemelzayat commented 1 month ago

@dstepanov done! https://github.com/kareemelzayat/demo-micronaut