Kotlin, Kapt and jpamodelgen


#1

Hi, I tried to generate metamodel using org.hibernate:hibernate-jpamodelgen and kapt, I followed this doc:
https://kotlinlang.org/docs/reference/kapt.html

but my metamodel wasn’t generated, follow my class and my build.gradle

build.gradle:
buildscript {
	ext {
		kotlinVersion = '1.3.21'
		springBootVersion = '2.1.2.RELEASE'
		dagger_version = '2.11'
	}
	repositories {
		jcenter()
		mavenCentral()
	}
	dependencies {
		classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
		classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}")
		classpath("org.jetbrains.kotlin:kotlin-allopen:${kotlinVersion}")
		classpath("org.jetbrains.kotlin:kotlin-noarg:${kotlinVersion}")
	}
}

apply plugin: "kotlin-allopen"
apply plugin: 'kotlin'
apply plugin: 'kotlin-spring'
apply plugin: 'kotlin-jpa'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'kotlin-kapt'
apply plugin: 'idea'

group = 'br.com.blog'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.9'

repositories {
	mavenCentral()
}

ext{
	junitVersion = '5.3.2'
	junitLaucherVersion ='1.3.2'
}

test{
	useJUnitPlatform()
	dependsOn 'cleanTest'

	testLogging {
		events "passed", "skipped", "failed"
	}
	afterTest { desc, result ->
		logger.quiet "Executing test ${desc.name} [${desc.className}] with result: ${result.resultType}"
	}
	testLogging {
		exceptionFormat = 'full'
	}
}

dependencies {

	kapt "org.hibernate:hibernate-jpamodelgen"
	kaptTest "org.hibernate:hibernate-jpamodelgen"
	implementation "org.hibernate:hibernate-jpamodelgen"

	implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
	implementation 'org.springframework.boot:spring-boot-starter-web'
	implementation 'com.fasterxml.jackson.module:jackson-module-kotlin'
	implementation 'org.flywaydb:flyway-core'
	implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
	implementation "org.jetbrains.kotlin:kotlin-reflect"

	runtimeOnly 'org.springframework.boot:spring-boot-devtools'
	runtimeOnly 'com.h2database:h2'
	runtimeOnly 'org.postgresql:postgresql'

	testImplementation ("org.springframework.boot:spring-boot-starter-test"){
		exclude module: "junit"
	}
	testCompile "com.h2database:h2:1.4.197"
	testCompile "org.junit.jupiter:junit-jupiter-api:${junitVersion}"
	testCompile "org.junit.jupiter:junit-jupiter-engine:${junitVersion}"
	testCompile "org.junit.jupiter:junit-jupiter-params:${junitVersion}"
	testCompile "org.junit.platform:junit-platform-launcher:${junitLaucherVersion}"
}

compileKotlin {
	kotlinOptions {
		freeCompilerArgs = ['-Xjsr305=strict']
		jvmTarget = '1.8'
	}
}

compileTestKotlin {
	kotlinOptions {
		freeCompilerArgs = ['-Xjsr305=strict']
		jvmTarget = '1.8'
	}
}

tag.lt:

package br.com.blog.blogapi.model

import javax.persistence.Column
import javax.persistence.Entity
import javax.persistence.GeneratedValue
import javax.persistence.GenerationType
import javax.persistence.Id
import javax.persistence.ManyToMany
import javax.persistence.Table
import javax.validation.constraints.NotNull
import javax.validation.constraints.Size

@Entity
@Table(name = "tags")
data class Tag (

        @Column(nullable = false, length = 140)
        @NotNull
        @Size(min = 2, max = 140)
        var descricao: String

){
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    var codigo : Long = 0

    @ManyToMany
    var posts: List<Post> = emptyList()

}

post.kt:

package br.com.blog.blogapi.model

import java.time.LocalDateTime
import javax.persistence.CascadeType
import javax.persistence.Column
import javax.persistence.Entity
import javax.persistence.GeneratedValue
import javax.persistence.GenerationType
import javax.persistence.Id
import javax.persistence.JoinColumn
import javax.persistence.JoinTable
import javax.persistence.ManyToMany
import javax.persistence.ManyToOne
import javax.persistence.PrePersist
import javax.persistence.PreUpdate
import javax.persistence.Table
import javax.validation.constraints.NotNull
import javax.validation.constraints.Size

@Entity
@Table(name = "POSTS")
data class Post (

        @Column(nullable = false, length = 140)
        @NotNull
        @Size(min = 2, max = 140)
        var titulo : String,

        @Column(nullable = false)
        @NotNull
        var descricao: String,

        @ManyToOne
        @JoinColumn(name = "codigo_pessoa")
        var pessoa: Pessoa
){
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    var codigo : Long = 0

    @ManyToMany(cascade = [CascadeType.MERGE, CascadeType.PERSIST])
    @JoinTable(name = "POSTS_TAGS",
            joinColumns = [JoinColumn(name = "CODIGO_POST")],
            inverseJoinColumns = [JoinColumn(name = "CODIGO_TAG")])
    var tags: List<Tag> = emptyList()

    @Column
    var dataEdicao: LocalDateTime? = null

    @Column
    var dataCriacao: LocalDateTime? = null

    @PrePersist
    fun onCreate(){
        dataCriacao = LocalDateTime.now()
    }

    @PreUpdate
    fun onUpdate(){
        dataEdicao = LocalDateTime.now()
    }

}

Pessoa.kt:

package br.com.blog.blogapi.model

import javax.persistence.CascadeType
import javax.persistence.Column
import javax.persistence.Entity
import javax.persistence.GeneratedValue
import javax.persistence.GenerationType
import javax.persistence.Id
import javax.persistence.OneToMany
import javax.persistence.Table
import javax.validation.constraints.Email
import javax.validation.constraints.NotNull
import javax.validation.constraints.Size


@Entity
@Table(name = "pessoas")
data class Pessoa (

        @Column(nullable = false, length = 140)
        @NotNull
        @Size(min = 2, max = 140)
        var nome : String,

        @Column(nullable = false, length = 140)
        @NotNull
        @Email
        @Size(min = 2, max = 140)
        var email : String,

        @Column(nullable = false, length = 40)
        @NotNull
        @Size(min = 8, max = 40)
        var senha : String
){
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    var codigo : Long = 0

    @OneToMany(mappedBy = "pessoa", cascade = [CascadeType.ALL], orphanRemoval = true)
    var posts : List<Post> = emptyList()
}

thank you


#2

Someone have some idea ?


#3

I tried to use a plugin with maven and the plugin generated normally my metamodels, how can I use the same configuration in gradle?

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.2.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>br.com.blog</groupId>
	<artifactId>blog-api</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>blog-api</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
		<kotlin.version>1.3.21</kotlin.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>com.fasterxml.jackson.module</groupId>
			<artifactId>jackson-module-kotlin</artifactId>
		</dependency>
		<dependency>
			<groupId>org.flywaydb</groupId>
			<artifactId>flyway-core</artifactId>
		</dependency>
		<dependency>
			<groupId>org.jetbrains.kotlin</groupId>
			<artifactId>kotlin-reflect</artifactId>
		</dependency>
		<dependency>
			<groupId>org.jetbrains.kotlin</groupId>
			<artifactId>kotlin-stdlib-jdk8</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>com.h2database</groupId>
			<artifactId>h2</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.postgresql</groupId>
			<artifactId>postgresql</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-jpamodelgen</artifactId>
		</dependency>
	</dependencies>

	<build>
		<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
		<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
			<plugin>
				<groupId>org.jetbrains.kotlin</groupId>
				<artifactId>kotlin-maven-noarg</artifactId>
				<version>${kotlin.version}</version>
				<configuration>
					<compilerPlugins>
						<plugin>jpa</plugin>
					</compilerPlugins>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.jetbrains.kotlin</groupId>
				<artifactId>kotlin-maven-plugin</artifactId>
				<configuration>
					<args>
						<arg>-Xjsr305=strict</arg>
					</args>
					<compilerPlugins>
						<plugin>spring</plugin>
					</compilerPlugins>
				</configuration>
				<dependencies>
					<dependency>
						<groupId>org.jetbrains.kotlin</groupId>
						<artifactId>kotlin-maven-allopen</artifactId>
						<version>${kotlin.version}</version>
					</dependency>
				</dependencies>
			</plugin>
			<plugin>
				<artifactId>kotlin-maven-plugin</artifactId>
				<groupId>org.jetbrains.kotlin</groupId>
				<version>${kotlin.version}</version>
				<executions>
					<execution>
						<id>kapt</id>
						<goals>
							<goal>kapt</goal>
						</goals>
						<configuration>
							<sourceDirs>
								<sourceDir>src/main/kotlin</sourceDir>
								<sourceDir>src/test/kotlin</sourceDir>
							</sourceDirs>
							<annotationProcessorPaths>
								<annotationProcessorPath>
									<groupId>org.hibernate</groupId>
									<artifactId>hibernate-jpamodelgen</artifactId>
									<version>5.3.7.Final</version>
								</annotationProcessorPath>
							</annotationProcessorPaths>
						</configuration>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>

</project>