11

When I create an Entity class using Java JPA static meta models are generated.

If I convert my Entities to Kotlin JPA static metamodels are not generated.

How to solve this problem?

EDIT

I am using Gradle as build tool.

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
A0__oN
  • 8,740
  • 6
  • 40
  • 61

3 Answers3

7

I had to use kapt plugin.

I had to add following line to my build.gradle file.

kapt "org.hibernate:hibernate-jpamodelgen:${hibernate_version}"
A0__oN
  • 8,740
  • 6
  • 40
  • 61
7

When using Maven, add the following snippet to <executions> of kotlin-maven-plugin.

<execution>
   <id>kapt</id>
   <goals>
      <goal>kapt</goal>
   </goals>
   <configuration>
      <sourceDirs>
         <sourceDir>src/main/kotlin</sourceDir>
      </sourceDirs>
      <annotationProcessorPaths>
         <annotationProcessorPath>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-jpamodelgen</artifactId>
            <version>5.3.2.Final</version>
         </annotationProcessorPath>
      </annotationProcessorPaths>
   </configuration>
</execution>
Juraj Mlich
  • 678
  • 1
  • 7
  • 15
3

I recommend this library. You can write queries like querydsl without using a static meta model while managing the jpa entity. https://github.com/line/kotlin-jdsl

Add Hibernate Kotlin JDSL and Hibernate to dependencies

dependencies {
    implementation("com.linecorp.kotlin-jdsl:hibernate-kotlin-jdsl:x.y.z")
    implementation("org.hibernate:hibernate-core:x.y.z")
}

and use it like this

val books: List<Book> = queryFactory.listQuery {
    select(entity(Book::class))
    from(entity(Book::class))
    where(column(Book::author).equal("Shakespeare"))
}

and expression

val max = max(column(Book::price))
val count = count(column(Book::price))
val greatest = greatest(column(Book::createdAt))

and cross join example

val books = queryFactory.listQuery<Book> {
    select(entity(Book::class))
    from(entity(Book::class))
    join(entity(Author::class) on(column(Book::authorId).equal(column(Author::id))))
    // ...
}