JPA Static Metamodel not recognized by IntelliJ
Asked Answered
V

5

15

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?

Ventail answered 14/12, 2017 at 15:38 Comment(2)
Recognize means the IDE doesn't add in the meta model classes in classpath or to the IDE generate the meta model classes on build process?Whangee
IDE generates the metamodels. I can run my application but IntelliJ shows error when I open file that uses these metamodelsVentail
V
7

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")
    }
}
Ventail answered 20/12, 2017 at 10:1 Comment(1)
right click on target/generated-sources and select Generated Sources Root from Mark Directory as option.Senescent
B
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.

Bless answered 18/12, 2017 at 5:30 Comment(1)
I am using gradle which is explicitly mentioned on question.Ventail
H
8

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>

Hollerman answered 25/9, 2020 at 22:31 Comment(0)
V
7

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")
    }
}
Ventail answered 20/12, 2017 at 10:1 Comment(1)
right click on target/generated-sources and select Generated Sources Root from Mark Directory as option.Senescent
D
3

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

Demosthenes answered 23/12, 2022 at 16:43 Comment(0)
W
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>
Whangee answered 17/12, 2017 at 14:50 Comment(1)
I don't META-INF folderVentail

© 2022 - 2025 — McMap. All rights reserved.