当我尝试调用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中不是本机或抽象的方法中的Absent Code属性”
如果你的pom中有一个javaee依赖项
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>6.0</version>
</dependency>
将它移动到依赖项的末尾。您的JPA依赖项必须在javaee依赖项之前,否则您将收到该错误。
发生的事情是你的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>
选择here的最新依赖
通常,直接从Oracle下载JavaEE JAR就足够了:
http://www.mkyong.com/maven/how-to-download-j2ee-api-javaee-jar-from-maven/