A0__oN
A0__oN

Reputation: 9120

Koitlin support for JPA static metamodel

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.

Upvotes: 12

Views: 4428

Answers (3)

dongsik.shin
dongsik.shin

Reputation: 39

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))))
    // ...
}

Upvotes: 3

Juraj Mlich
Juraj Mlich

Reputation: 688

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>

Upvotes: 8

A0__oN
A0__oN

Reputation: 9120

I had to use kapt plugin.

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

kapt "org.hibernate:hibernate-jpamodelgen:${hibernate_version}"

Upvotes: 11

Related Questions