Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA Static Metamodel not recognized by IntelliJ

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?

like image 277
A0__oN Avatar asked Dec 14 '17 15:12

A0__oN


3 Answers

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.

like image 154
Thorben Janssen Avatar answered Nov 04 '22 20:11

Thorben Janssen


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>

like image 7
gabrieeel Avatar answered Nov 04 '22 20:11

gabrieeel


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")
    }
}
like image 6
A0__oN Avatar answered Nov 04 '22 22:11

A0__oN