当前位置: 代码迷 >> java >> Hibernate JPA @Generated注释编译错误
  详细解决方案

Hibernate JPA @Generated注释编译错误

热度:22   发布时间:2023-07-31 11:56:22.0

我正在使用Eclipse启动Maven /休眠项目,而我的元模型类却遇到了问题。

我在处理@Generated注释的行上得到了一个红色的x:

import javax.annotation.Generated;
@Generated(value = "org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor")

我已经阅读了 ,并介绍了addGeneratedAnnotation选项:

If set to true the processor will add the @Generated to the generated Java source file. Adding this annotation using JDK 5 will cause a compilation error. In this case set the flag to false. The default for this option is true

我在eclipse中的哪个位置设置该选项? 在运行配置? 我试过了,只做了一个Maven编译,但是并没有解决。 哪个目标将生成新的元模型类。

我很确定我已经正确设置了构建路径和工厂路径。 我正在使用Java 9。

我的pom.xml文件包含以下内容

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-jpamodelgen</artifactId>
    <version>5.3.1.Final</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>5.3.1.Final</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>5.3.1.Final</version>
</dependency>

更新

我尝试在maven中运行package并进行compile ,但版本错误为52.0 / 53.0。

has been compiled by a more recent version of the Java Runtime (class file version 53.0), this version of the Java Runtime only recognizes class file versions up to 52.0

我能够通过将编译器级别和项目方面Java级别设置为1.8来解决。

有没有办法在更高版本中进行此设置?

如果您使用的是Maven,则需要导入休眠库

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-jpamodelgen</artifactId>
    <version>{hibernate.version}</version>
    <scope>provided</scope>
</dependency>

对于gradle

org.hibernate:hibernate-core:5.4.1.Final

或手动将休眠库添加到项目中

尝试在您的pom.xml中添加此Maven依赖项

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-jpamodelgen</artifactId>
    <version>{hibernate.version}</version>
    <scope>provided</scope>
</dependency>

之后,删除类的@Generated并执行:

 mvn package

Modelgen将在目标/ ...中生成您的元模型。

元模型已生成的本地应该在您的类路径中。

  相关解决方案