我使用Gradle作为构建工具,用JHipster生成了这个应用程序。
当我创建实体时,我添加了过滤支持,这将生成JPA静态元模型。但是IntelliJ无法识别这些元模型。
我在IntelliJ中启用了注解处理器设置,但似乎不起作用。
我需要更改哪些设置才能使IntelliJ识别JPA静态元模型?
我使用Gradle作为构建工具,用JHipster生成了这个应用程序。
当我创建实体时,我添加了过滤支持,这将生成JPA静态元模型。但是IntelliJ无法识别这些元模型。
我在IntelliJ中启用了注解处理器设置,但似乎不起作用。
我需要更改哪些设置才能使IntelliJ识别JPA静态元模型?
默认情况下,元模型类将生成到/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技巧中更详细地解释了这个问题。
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")
}
}
我不能发表评论,但我想在Thorben Janssen的回答中补充一点。
除了插件配置之外,我还需要将项目依赖项中添加这个:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>5.4.21.Final</version>
</dependency>
<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>
对我来说,这不是配置文件的问题(上述解决方案都没有起作用),而是我需要重新加载所有Maven项目文件。
在IntelliJ Idea中进行如下操作:
编译
重新加载所有Maven项目
现在,元类(例如SampleClass_
)应该可以被导入并被IntelliJ识别。
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>