JPA静态元模型在IntelliJ中未被识别。

13

我使用Gradle作为构建工具,用JHipster生成了这个应用程序。

当我创建实体时,我添加了过滤支持,这将生成JPA静态元模型。但是IntelliJ无法识别这些元模型。

我在IntelliJ中启用了注解处理器设置,但似乎不起作用。

我需要更改哪些设置才能使IntelliJ识别JPA静态元模型?


识别的意思是IDE不会将元模型类添加到类路径中,还是IDE在构建过程中生成元模型类? - Henrique Fernandes Cipriano
1
IDE生成元模型。我可以运行我的应用程序,但是当我打开使用这些元模型的文件时,IntelliJ会显示错误。 - A0__oN
5个回答

8

默认情况下,元模型类将生成到/target/generated-sources/annotations文件夹中。似乎该文件夹未注册为源文件夹。

您可以在IDE中手动更改,或者如果使用Maven构建,则可以通过将以下插件添加到构建配置中自动完成:

<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>

我在我的Hibernate技巧中更详细地解释了这个问题。


3
我正在使用明确提到的Gradle构建工具。 - A0__oN

7
为了让IntelliJ IDEA识别生成的类,我需要在build.gradle文件中添加这一行。
sourceSets {
    main.java.srcDirs += 'build/generated/source/apt/main'
}

更新

更好的解决方案是修改IntelliJ插件。

idea {
    module {
        sourceDirs += file("build/generated/source/apt/main")
        generatedSourceDirs += file("build/generated/source/apt/main")
    }
}

target/generated-sources上右键单击,然后从标记目录为选项中选择Generated Sources Root - undefined

7

我不能发表评论,但我想在Thorben Janssen的回答中补充一点。

除了插件配置之外,我还需要将项目依赖项中添加这个:

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

这就是在目标/generated-sources/annotations路径下生成源文件的内容。
因此,pom最终变成了这样:
    <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>


1

对我来说,这不是配置文件的问题(上述解决方案都没有起作用),而是我需要重新加载所有Maven项目文件。

在IntelliJ Idea中进行如下操作:

  1. 转到IDE右侧的Maven选项卡(您可能需要在“查看” -> “工具窗口”下使其可见)
  2. 打开项目并进行编译
  3. 在选项卡左上角按重新加载所有Maven项目

现在,元类(例如SampleClass_)应该可以被导入并被IntelliJ识别。


0

Intellij的构建识别此文件中列出的所有处理器:

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

.

如果您使用Eclipse Link,请在文件中包含以下行:

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

案例 Hibernate:

org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor

确保您拥有所有依赖项:我将以Maven为例进行描述:

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

或者

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

我没有META-INF文件夹。 - A0__oN

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接