1

I am using java 8, spring boot 2.0.0, spring-data-jpa(spring-boot-starter-data-jpa), gradle, intellij. I've been trying to use JPA Metamodel, but having difficulty on finding how to configure.

Metamodels for Entity classes aren't just generated.

I guessed it would be simple, but now it seems that can be wrong. How can I use it?

jjangga
  • 441
  • 5
  • 14

2 Answers2

7

JDK11 / Gradle 5.0 / Hibernate 5.3.7.Final

sourceSets.main.java.srcDirs += "${buildDir}/generated"

compileJava {
  options.annotationProcessorGeneratedSourcesDirectory = file("${buildDir}/generated")
}

dependencies {
  annotationProcessor("javax.xml.bind:jaxb-api")
  annotationProcessor("org.hibernate:hibernate-jpamodelgen")
}

Generated Metamodel classes will be generated at 'build/generated'

If you are using JDK8 or Hibernate 5.4+, annotationProcessor("javax.xml.bind:jaxb-api") may unnecessary.

Yuki Yoshida
  • 1,233
  • 1
  • 15
  • 28
  • 1
    You saved my day! I couldn't generate anything for two days and for some reason removing the explicit version of the annotation processor made it work. I still can't figure out why, but now everything works – Nikita Kobtsev Apr 13 '22 at 10:12
  • Me 3. After hours spent proverbially dancing around the same issue, turns out that removing the explicit version made it work. – Dark Star1 Nov 01 '22 at 14:32
1

I did this the other day using the scalified metamodel gradle plugin (https://plugins.gradle.org/plugin/com.scalified.plugins.gradle.metamodel). I'm using Spring Boot 2.0.5, but I don't see why it wouldn't work the same with Spring Boot 2.0.0. I'm using Gradle 4.8.1 as well.

Below is an excerpt of my build.gradle.

buildscript {
repositories {
   maven {
     url "https://plugins.gradle.org/m2/"
   }
}
dependencies {
  classpath (
      "org.springframework.boot:spring-boot-gradle-plugin:2.0.0",
      "gradle.plugin.com.scalified.plugins.gradle:metamodel:0.0.1");
  }
}

apply plugin: "com.scalified.plugins.gradle.metamodel"

// The plugin will default to the latest version of Hibernate if this is not specified
metamodel {
   hibernateVersion = '5.2.14.Final' // For Spring Boot 2.0.0
   hibernateVersion = '5.2.17.Final' // For Spring Boot 2.0.5
}

This builds the metamodal files under src/generated and they can be used in your code. I had to also change an IntelliJ setting because IntelliJ's Build Automatically excludes some Gradle tasks that could be long running. See Automatically run Gradle task in project build with IntelliJ IDEA and https://youtrack.jetbrains.com/issue/IDEA-175165 for more details.

This setting I changed to overcome this is: Preferences->Build/Execution/Deployment->Gradle->Runner->Delegate IDE build/run actions to Gradle. An alternative would be to run the metamodelCompile gradle task manually as needed. That would lessen the time to rebuild by a little if you aren't frequently change your entities.

PCalouche
  • 1,605
  • 2
  • 16
  • 19