使用 JPA 时出现“java.lang.NoSuchFieldError”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6295389/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
'java.lang.NoSuchFieldError' while using the JPA
提问by Bhushan
I am getting the following error while trying out this JPA tutorial:
尝试此JPA 教程时出现以下错误:
java.lang.NoSuchFieldError: NONE
at org.hibernate.ejb.QueryImpl.<init>(QueryImpl.java:609)
at org.hibernate.ejb.QueryImpl.<init>(QueryImpl.java:80)
at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:272)
at entity.PersonTest.insertAndRetrieve(PersonTest.java:50)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="examplePersistenceUnit" transaction-type="RESOURCE_LOCAL">
<class>entity.Person</class>
<properties>
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.format_sql" value="false" />
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/jpa_test" />
<property name="hibernate.connection.username" value="root" />
<property name="hibernate.connection.password" value="admin" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="create" />
</properties>
</persistence-unit>
</persistence>
0(ParentRunner.java:42)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Here is my persistence.xml:
这是我的persistence.xml:
package entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
public class Person {
@Id
@GeneratedValue
private int id;
private String firstName;
private char middleInitial;
private String lastName;
private String streetAddress1;
private String streetAddress2;
private String city;
private String state;
private String zip;
.
.
.
(getters and setters)
Person.java
人.java
@SuppressWarnings("unchecked")
@Test
public void insertAndRetrieve() {
em.getTransaction().begin();
em.persist(p1);
em.persist(p2);
em.getTransaction().commit();
final List<Person> list = em.createQuery("select p from Person p")
.getResultList();
System.out.println("here...");
assertEquals(2, list.size());
Part of my JUnit Test file:
我的 JUnit 测试文件的一部分:
em.getTransaction().begin();
em.persist(p1);
em.persist(p2);
em.getTransaction().commit();
I am using MySQL db, the 'person' table is present in the database. And after
我正在使用 MySQL db,数据库中存在“person”表。之后
antlr-2.7.6.jar,
commons-collections-3.1.jar,
dom4j-1.6.1.jar, hibernate3.jar,
hibernate-annotations-3.5.5-Final.jar,
hibernate-commons-annotations-3.2.0.Final.jar,
hibernate-entitymanager-3.5.5-Final.jar,
hibernate-entitymanager-3.5.5-Final-sources.jar,
javaee-api-5.0-3.jar,
javassist-3.9.0.GA.jar,
log4j-1.2.12.jar,
mysql-connector-java-5.1.15.jar,
slf4j-api-1.6.1.jar,
slf4j-simple-1.6.1.jar,
hibernate-jpa-2.0-api-1.0.0-CR-1.jar
I can see the two records are inserted in the person table. But on the next statement, it fails.
我可以看到两条记录被插入到 person 表中。但是在下一个语句中,它失败了。
EDIT 1:JARs in my build path:
编辑 1:我的构建路径中的 JAR:
##代码##Can anyone tell me what I am doing wrong?
谁能告诉我我做错了什么?
Thanks.
谢谢。
回答by MikeTheReader
Looks like you're having a similar issue to this:
看起来您遇到了与此类似的问题:
"java.lang.NoSuchFieldError: NONE" in hibernate with Spring 3, maven, JPA, c3p0
使用 Spring 3、maven、JPA、c3p0 休眠的“java.lang.NoSuchFieldError: NONE”
回答by Vineet Reynolds
Remove the ejb3-persistence.jar JAR file from the class path. It contains the interfaces of the JPA 1.0 specification. You'll need to retain the hibernate-jpa-2.0-api-1.0.0-CR-1.jar JAR file, for it contains the interfaces for the JPA 2.0 specification.
从类路径中删除 ejb3-persistence.jar JAR 文件。它包含 JPA 1.0 规范的接口。您需要保留 hibernate-jpa-2.0-api-1.0.0-CR-1.jar JAR 文件,因为它包含 JPA 2.0 规范的接口。
Note: If you need to work against JPA 1.0, then do not use Hibernate 3.5.5 for it contains the JPA 2.0 implementation. In the future, use Maven for dependency management; it will help you avoid these issues.
注意:如果您需要针对 JPA 1.0 工作,则不要使用 Hibernate 3.5.5,因为它包含 JPA 2.0 实现。未来使用Maven进行依赖管理;它将帮助您避免这些问题。
Addendum:
附录:
If I were to setup Hibernate EntityManager 3.5.5 (JPA 2) from scratch, these would be the files I would consider. The following list has been inferred from the Hibernate documentation:
如果我要从头开始设置 Hibernate EntityManager 3.5.5 (JPA 2),这些将是我会考虑的文件。以下列表是从Hibernate 文档中推断出来的:
- hibernate3.jar
- hibernate-jpa-2.0-api-1.0.0.Final.jar
- antlr-2.7.6.jar
- commons-collections-3.1.jar
- dom4j-1.6.1.jar
- javassist-3.9.0.GA.jar
- jta-1.1.jar
- slf4j-api-1.5.8.jar and either of
- cglib-2.2.jar, or
- javassist-3.9.0.GA.jar
- hibernate3.jar
- hibernate-jpa-2.0-api-1.0.0.Final.jar
- antlr-2.7.6.jar
- commons-collections-3.1.jar
- dom4j-1.6.1.jar
- javassist-3.9.0.GA.jar
- jta-1.1.jar
- slf4j-api-1.5.8.jar 和
- cglib-2.2.jar,或
- javassist-3.9.0.GA.jar
The last two haven't been mentioned in the documentation, but are required. I don't see the need for any of the other JARs, except for mysql-connector-java-5.1.15.jar and log4j-1.2.12.jar (both of which could be retained).
文档中没有提到最后两个,但它们是必需的。我认为不需要任何其他 JAR,除了 mysql-connector-java-5.1.15.jar 和 log4j-1.2.12.jar(两者都可以保留)。