我以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>
我在我的冬眠技巧之一中解释了更多细节。
要获得Intellij Idea以识别生成的类,我必须在build.gradle
sourceSets {
main.java.srcDirs += 'build/generated/source/apt/main'
}
update
更好的解决方案是修改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>
这就是在目标/生成的源/注释路径中生成源的原因。
所以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>
Intellij的构建识别此文件中列出的所有处理器:
meta-inf/services/javax.annotation.antocessing.processor
。
案例您使用Eclipse链接,在文件中包含此行:
org.eclipse.persistence.internal.jpa.modelgen.caronicalmodelprocessor
案例冬眠:
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>
对我来说,这不是配置文件的问题(上述解决方案都没有使用),但是我只需重新加载所有Maven项目文件。
在Intellij Idea中为此:
- 转到IDE右侧的Maven选项卡(您可能必须使其在视图 - &gt;工具窗口中可见) )
- 打开项目和
compile
- 在选项卡的左上角
Reload all Maven Projects
现在,元类(例如 SampleClass_
)应导入并由Intellij