13

I generated the application with JHipster with Gradle as the build tool.

When I created entity I added filtering support, which generated JPA static metamodel. But IntelliJ doesn't recognize the metamodels.

I have enabled the annotation processor settings on IntelliJ but it doesn't seem to work.

What settings do I have to change for IntelliJ to recognize the JPA static metamodels?

A0__oN
  • 8,740
  • 6
  • 40
  • 61

5 Answers5

8

By default, the metamodel classes get generated into the /target/generated-sources/annotations folder. It seems like that folder isn't registered as a source folder.

You can either change that manually in your IDE or if you're using a Maven build, you can do that automatically by adding the following plugin to your build configuration:

<project>
    ...

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>3.0.0</version>
                <executions>
                    <execution>
                        <id>add-source</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>add-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <source>target/generated-sources/annotations</source>
                            </sources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    ...
</project>

I explained that in more details in one of my Hibernate Tips.

Thorben Janssen
  • 3,012
  • 9
  • 23
7

I'm not allowed to comment but I wanted to add to Thorben Janssen's answer. Besides the plugin config I also had to add this to the dependencies of the project:

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-jpamodelgen</artifactId>
            <version>5.4.21.Final</version>
        </dependency>

This is what generates the sources in the target/generated-sources/annotations path.

So the pom ended up like this:

    <dependencies>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-jpamodelgen</artifactId>
            <version>5.4.21.Final</version>
        </dependency>
        ...
    </dependencies>

    ...

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>3.2.0</version>
                <executions>
                    <execution>
                        <id>add-source</id>
                        <goals>
                            <goal>add-source</goal>
                        </goals>
                        <phase>generate-sources</phase>
                        <configuration>
                            <sources>
                                <source>target/generated-sources/annotations</source>
                            </sources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
        ...
    </build>

gabrieeel
  • 113
  • 1
  • 7
6

To get IntelliJ IDEA to recognize the generated classes, I had to add this line on build.gradle

sourceSets {
    main.java.srcDirs += 'build/generated/source/apt/main'
}

Update

Better solution is to modify IntelliJ Plugin

idea {
    module {
        sourceDirs += file("build/generated/source/apt/main")
        generatedSourceDirs += file("build/generated/source/apt/main")
    }
}
A0__oN
  • 8,740
  • 6
  • 40
  • 61
0

Intellij's build recognize all processors listed in this file:

META-INF/services/javax.annotation.processing.Processor

.

Case you use Eclipse Link, include this line inside the file:

org.eclipse.persistence.internal.jpa.modelgen.CanonicalModelProcessor

Case Hibernate:

org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor

Ensure that you have all dependencys: I will describe using maven just for example:

<dependency>
    <groupId>org.eclipse.persistence</groupId>
    <artifactId>org.eclipse.persistence.jpa.modelgen.processor</artifactId>
        <version>2.7.0</version>
        <scope>provided</scope>
</dependency>

OR

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-jpamodelgen</artifactId>
    <version>5.2.12.Final</version>
    <scope>provided</scope>
</dependency>
0

For me it wasn't a problem of the configuration files (none of the above mentioned solutions worked), but I simply had to reload all Maven project files.

For this in IntelliJ Idea:

  1. Go to the Maven tab on the right side of the IDE (you might have to make it visible under View -> Tool Windows)
  2. Open the project and compile
  3. On the top left corner of the tab press Reload all Maven Projects

Now, the meta classes (e.g. SampleClass_) should be importable and recognized by IntelliJ