JPA java.lang.ClassFormatError问题

时间:2016-10-16 09:01:25

标签: java jpa

启动JPA和Derby项目时遇到问题。我有以下错误:

Exception in thread "main" java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/persistence/Persistence

我的Main.java是:

public class Main {
    private static final String PERSISTENCE_UNIT_NAME = "customers";
    private static EntityManagerFactory factory;
public static void main(String[] args) {
    factory = Persistence.createEntityManagerFactory("todos");

    EntityManager em = factory.createEntityManager();

    Query q = em.createQuery("select t from CUSTOMER t");
    List<Customer> customerList = q.getResultList();
    for (Customer customer : customerList) {
        System.out.println(customer);
    }
    System.out.println("Size: " + customerList.size());

    em.getTransaction().begin();
    Customer customer = new Customer();
    customer.setCity("Warsaw");
    customer.setCountry("USA");
    customer.setName("John");
    customer.setStreet("Street");
    em.persist(customer);
    em.getTransaction().commit();

    em.close();
}
}

我的persistence.xml

<?xml version="1.0" encoding="UTF-8" ?>
<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" xmlns="http://java.sun.com/xml/ns/persistence">
    <persistence-unit name="todos" transaction-type="RESOURCE_LOCAL">
        <class>b.model.Product</class>
        <class>b.model.Book</class>
        <class>b.model.Movie</class>
        <class>b.model.Customer</class>
        <class>b.model.Order</class>
        <class>b.model.OrderElement</class>
        <properties>
            <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
            <property name="javax.persistence.jdbc.url"
                      value="jdbc:derby://localhost:1527/baza;create=true" />


        </properties>

    </persistence-unit>
</persistence>

我的项目中有2个依赖项:

  1. derbyclient-10.12.1.1.jar
  2. 的JavaEE-API-6.0.jar
  3. 关于我做错什么的任何想法?

    我将所有Model类(如Customer)定义如下:

    @Data
    @Entity(name = "CUSTOMER")
    public class Customer {
    
        @Id
        @Column(name = "CUSTOMER_ID", nullable = false)
        @GeneratedValue(strategy = GenerationType.AUTO)
        private long customerId;
    
        @Column(name = "NAME")
        private String name;
    
        @Column(name = "SURNAME")
        private String surname;
    
        @Column(name = "STREET")
        private String street;
    
        @Column(name = "CITY")
        private String city;
    
        @Column(name = "ZIP_CODE")
        private String zipCode;
    
        @Column(name = "COUNTRY")
        private String country;
    
        @OneToMany(mappedBy="customer",targetEntity=Order.class, fetch= FetchType.EAGER)
        private Collection orders;
    
    }
    

1 个答案:

答案 0 :(得分:0)

这很可能是因为在WAR中包装了javaee-api jar。它只包含方法签名,但没有方法体,因此无法使用它运行/测试/部署应用程序。它的唯一目的是针对它编译您的应用程序,从而确保您遵循javaee API规范并避免依赖于具体实现。此外,不得打包服务器在WAR中提供的任何API。