JPA ClassFormat 错误“类文件 javax/persistence/Persistence 中不是本机或抽象的方法中缺少代码属性”

发布于 2024-12-20 16:21:59 字数 755 浏览 1 评论 0原文

当我尝试调用 100% 工作代码时,我收到 Eclipse 错误。例如,它在我的 netbeans 中工作,但不在这个 eclipse 项目中工作。该错误很荒谬,我几乎可以肯定它是由我正在使用的 OPEN JPA 的某些 Maven 依赖项引起的。有什么指点吗?

Map<String,String> properties = new HashMap<String,String>();
properties.put(PersistenceUnitProperties.JDBC_PASSWORD, "");
properties.put(PersistenceUnitProperties.JDBC_USER, "root");
properties.put(PersistenceUnitProperties.JDBC_URL, "jdbc:mysql://localhost:3306/mydb");
properties.put(PersistenceUnitProperties.JDBC_DRIVER, "com.mysql.jdbc.Driver");

emf = Persistence.createEntityManagerFactory("Persistentunitname", properties);

错误发生在最后一行,错误是:

ClassFormat 错误“类文件 javax/persistence/Persistence 中不是本机或抽象的方法中缺少代码属性”

I get error from eclipse when I try to invoke a 100% working code. It is for example working in my netbeans but not this eclipse project. The error is absurd and I am almost sure it's caused by some Maven dependency for OPEN JPA that I'm using. Any pointers?

Map<String,String> properties = new HashMap<String,String>();
properties.put(PersistenceUnitProperties.JDBC_PASSWORD, "");
properties.put(PersistenceUnitProperties.JDBC_USER, "root");
properties.put(PersistenceUnitProperties.JDBC_URL, "jdbc:mysql://localhost:3306/mydb");
properties.put(PersistenceUnitProperties.JDBC_DRIVER, "com.mysql.jdbc.Driver");

emf = Persistence.createEntityManagerFactory("Persistentunitname", properties);

The error occurs on the last line, and the error is:

ClassFormat Error "Absent Code attribute in method that is not native or abstract in class file javax/persistence/Persistence"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

热血少△年 2024-12-27 16:21:59

如果您的 pom 中有 javaee 依赖项,

 <dependency>
  <groupId>javax</groupId>
  <artifactId>javaee-web-api</artifactId>
  <version>6.0</version>
</dependency>

请将其移至依赖项的末尾。您的 JPA 依赖项必须位于 javaee 依赖项之前,否则您将收到该错误。

If you have a javaee dependency in your pom like

 <dependency>
  <groupId>javax</groupId>
  <artifactId>javaee-web-api</artifactId>
  <version>6.0</version>
</dependency>

move it to the end of your dependencies. Your JPA dependency must come before the javaee dependency or you will get that error.

澜川若宁 2024-12-27 16:21:59

发生的情况是您的 pom 引用了 javaee-api。该包不提供方法体,仅提供标头。它实际上是一个损坏的包,部署到 JavaEE 环境时会在运行时“修复”。

NetBeans 提供了 javaee 的真正实现,而 Eclipse 则不然。要解决此问题,请添加:

<dependency>
   <groupId>org.eclipse.persistence</groupId>
   <artifactId>eclipselink</artifactId>
   <version>2.4.0</version>
   <scope>compile</scope>
</dependency>

这将提供 javax.persistence 的必要实现,并且您的代码将起作用。

编辑:(更新了丢失的工件)

<dependency>
    <groupId>org.eclipse.persistence</groupId>
    <artifactId>eclipselink</artifactId>
    <version>2.5.0</version>
</dependency>

中选择最新的依赖项这里

What's happening is that your pom references javaee-api. This package doesn't provide method bodies, just headers. It's effectively a broken package that is 'fixed' at runtime when deployed to a JavaEE environment.

NetBeans is providing a real implementation of javaee whereas Eclipse is not. To solve this add:

<dependency>
   <groupId>org.eclipse.persistence</groupId>
   <artifactId>eclipselink</artifactId>
   <version>2.4.0</version>
   <scope>compile</scope>
</dependency>

This will provide the necessary implementations of javax.persistence and your code will work.

EDIT: (updated the missing artifact)

<dependency>
    <groupId>org.eclipse.persistence</groupId>
    <artifactId>eclipselink</artifactId>
    <version>2.5.0</version>
</dependency>

pick the latest dependency from here

德意的啸 2024-12-27 16:21:59

另一种无需更改 javaee api 的替代方案是首先在 Maven 依赖项块中声明我们需要的真实 api,如下所示:

  <dependencies>
    <dependency>
      <groupId>org.hibernate.javax.persistence</groupId>
      <artifactId>hibernate-jpa-2.0-api</artifactId>
      <version>${jpa-api.version}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-jdk14</artifactId>
      <version>{slf4j.version}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>javax.transaction</groupId>
      <artifactId>jta</artifactId>
      <version>${jta.version}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>javaee</groupId>
      <artifactId>javaee-api</artifactId>
      <version>${javaee-api.version}</version>
      <scope>provided</scope>
    </dependency>

    <!-- Another tests dependencies-->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>${junit.version}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.assertj</groupId>
      <artifactId>assertj-core</artifactId>
      <version>${assertj-core.version}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.mockito</groupId>
      <artifactId>mockito-core</artifactId>
      <version>${mockito-core.version}</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

通过这种方式,我们可以覆盖测试范围的类路径。我们可以在此处此处

Another alternative without change the javaee api, it's to declare the real api we need first in the maven dependencies block, something like this:

  <dependencies>
    <dependency>
      <groupId>org.hibernate.javax.persistence</groupId>
      <artifactId>hibernate-jpa-2.0-api</artifactId>
      <version>${jpa-api.version}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-jdk14</artifactId>
      <version>{slf4j.version}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>javax.transaction</groupId>
      <artifactId>jta</artifactId>
      <version>${jta.version}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>javaee</groupId>
      <artifactId>javaee-api</artifactId>
      <version>${javaee-api.version}</version>
      <scope>provided</scope>
    </dependency>

    <!-- Another tests dependencies-->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>${junit.version}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.assertj</groupId>
      <artifactId>assertj-core</artifactId>
      <version>${assertj-core.version}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.mockito</groupId>
      <artifactId>mockito-core</artifactId>
      <version>${mockito-core.version}</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

With this way, we override the classpath for our test scope. We can read about this maven behavior here and here.

海的爱人是光 2024-12-27 16:21:59

通常,直接从 Oracle 下载 JavaEE JAR 就足够了:

http://www.mkyong.com/maven/how-to-download-j2ee-api-javaee-jar-from-maven/

Typically it is enough to download the JavaEE JAR directly from Oracle:

http://www.mkyong.com/maven/how-to-download-j2ee-api-javaee-jar-from-maven/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文