micronaut-data icon indicating copy to clipboard operation
micronaut-data copied to clipboard

hibernate-jpa fails with Kotlin and Maven

Open burtbeckwith opened this issue 2 years ago • 5 comments

Expected Behavior

No response

Actual Behaviour

No response

Steps To Reproduce

  • Create an app with Kotlin, Maven, and feature hibernate-jpa: mn create-app com.example.demo --build=maven --lang=kotlin --features=hibernate-jpa)

  • Create an entity class, e.g.

package test

import javax.persistence.Column
import javax.persistence.Entity
import javax.persistence.GeneratedValue
import javax.persistence.GenerationType.AUTO
import javax.persistence.Id
import javax.persistence.Table

@Entity
@Table(name = "book")
data class Book(@Column(name = "name", nullable = false) var title: String) {
    @Id @GeneratedValue(strategy = AUTO) var id: Long? = null
}
  • Create a test:
package test

import io.micronaut.test.extensions.junit5.annotation.MicronautTest
import jakarta.inject.Inject
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import javax.persistence.EntityManager

@MicronautTest
class BookTest {

    @Inject
    lateinit var entityManager: EntityManager

    @Test
    fun test() {
        assertEquals(0,
                entityManager.createQuery("FROM Book", Book::class.java).resultList.size)
    }
}

Run the test and it fails with

Caused by: io.micronaut.context.exceptions.ConfigurationException: Entities not found for JPA configuration: 'default' within packages [org.junit.jupiter.engine.descriptor,test]. Check that you have correctly specified a package containing JPA entities within the "jpa.default.entity-scan.packages" property in your application configuration and that those entities are either compiled with Micronaut or a build time index produced with @Introspected(packages="foo.bar", includedAnnotations=Entity.class) declared on your Application class
	at io.micronaut.configuration.hibernate.jpa.EntityManagerFactoryBean.hibernateMetadataSources(EntityManagerFactoryBean.java:167)
	at io.micronaut.configuration.hibernate.jpa.$EntityManagerFactoryBean$HibernateMetadataSources1$Definition.doBuild(Unknown Source)
	at io.micronaut.context.AbstractInitializableBeanDefinition.build(AbstractInitializableBeanDefinition.java:707)
	at io.micronaut.context.BeanDefinitionDelegate.build(BeanDefinitionDelegate.java:132)
	at io.micronaut.context.DefaultBeanContext.doCreateBean(DefaultBeanContext.java:2336)
	... 71 more

Do the same with a Gradle app and it works fine.

Environment Information

No response

Example Application

No response

Version

3.3.4

burtbeckwith avatar Mar 07 '22 03:03 burtbeckwith

Are you mapped entity-scan in application.yml ? If you don't, plz try adding this below in application.yml:

jpa:
  default:
    entity-scan:
      packages: 'your.package.domain'

fernando-nog avatar Mar 07 '22 15:03 fernando-nog

I have entities marked by jakarta annotation and could reproduce the problem. entity-scan does not help

rlconst avatar Aug 02 '22 11:08 rlconst

Hibernate 5 supports only javax annotations

dstepanov avatar Aug 02 '22 11:08 dstepanov

I am encountering the same error while using Micronaut 3.8.9 + Maven + Groovy. When I build the application normally, there are no issues. However, when I try to build it as a JAR file, the error occurs: "Message: Entities not found for JPA configuration: 'default' within packages [vm o.du5.com.model]. Check that you have correctly specified a package containing JPA entities within the "jpa.default.entity-scan.packages" property in your appli cation configuration and that those entities are either compiled with Micronaut or a build time index produced with @Introspected(packages="foo.bar", includedAn notations=Entity.class) declared on your Application class". I am following this documents : https://guides.micronaut.io/latest/micronaut-jpa-hibernate-gradle-java.html. Please help. Thanks

phamvanlinh20111993 avatar Apr 22 '23 08:04 phamvanlinh20111993

I recently migrated a Java project to Kotlin and I had the same problem. Note that I am using Gradle. I added to the plugins { id("com.google.devtools.ksp") version "1.9.21-1.0.15" } and in the dependencies { ksp("io.micronaut.serde:micronaut-serde-processor:${micronautSerdeVersion}") ksp("io.micronaut.data:micronaut-data-processor:${micronautDataVersion}") ksp("io.micronaut.validation:micronaut-validation-processor:${micronautValidationVersion}")} . The three dependencies were previously annotationProcessor('etc...'). Changing them to ksp fixed the issue for me.

AchoVasilev avatar Dec 30 '23 11:12 AchoVasilev