2013-08-07 35 views
1

所以我想通過一些JPA的東西的工作,我得到這個錯誤:java.lang.NoClassDefFoundError JPA例如

Exception in thread "main" java.lang.NoClassDefFoundError: org.apache.commons.collections.set.MapBackedSet 
Caused by: java.lang.ClassNotFoundException: org.apache.commons.collections.set.MapBackedSet 

我主要取自網上這個代碼來獲得一個項目工作首先,我可以玩弄它並理解其運作方式。從做一些研究,我認爲問題的一部分是我缺少com.ibm.ws.jpa.thinclient.jar文件。但是我不知道我在哪裏可以找到。

我persistance.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="user" transaction-type="RESOURCE_LOCAL"> 
    <class>jpa.Main</class> 
    <properties> 
     <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver" /> 
     <property name="javax.persistence.jdbc.url" 
     value=****Derby URL**** /> 
     <property name="javax.persistence.jdbc.user" value="test" /> 
     <property name="javax.persistence.jdbc.password" value="test" /> 
    </properties> 

    </persistence-unit> 
</persistence> 

我User.Java

package jpa; 

import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 
import javax.persistence.Id; 

    @Entity 
    public class User { 
    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    private int userID; 
    private String login; 
    private String password; 
    public int getUserID() { 
     return userID; 
    } 
    public void setUserID(int userID) { 
     this.userID = userID; 
    } 
    public String getLogin() { 
     return login; 
    } 
    public void setLogin(String login) { 
     this.login = login; 
    } 
    public String getPassword() { 
     return password; 
    } 
    public void setPassword(String password) { 
     this.password = password; 
    } 

    @Override public String toString() { 
      return "Todo [summary=" + login + ", description=" + password + "]"; 

    } 
    } 

我main.java

package jpa; 

import java.util.List; 
import javax.persistence.EntityManager; 
import javax.persistence.EntityManagerFactory; 
import javax.persistence.Persistence; 
import javax.persistence.Query; 

public class Main { 

      private static final String PERSISTENCE_UNIT_NAME = "user"; 
      private static EntityManagerFactory factory; 
      public static void main(String[] args) { 
      factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME); 
      EntityManager em = factory.createEntityManager(); 
      // Read the existing entries and write to console 
      Query q = em.createQuery("select u from User u"); 
      List<User> userList = q.getResultList(); 
      for (User user : userList) { 
       System.out.println(user); 
      } 
      System.out.println("Size: " + userList.size()); 
      em.getTransaction().begin(); 
      User user = new User(); 
      user.setUserID(12); 
      user.setPassword("This is a test"); 
      em.persist(user); 
      em.getTransaction().commit(); 
      em.close(); 
      } 
     } 
+0

您確定commons-collections jar在運行時可用於容器嗎? – chrylis

+0

您使用的是JPA實現?基於Apache OpenJPA的WebSphere JPA解決方案? –

+0

@chrylis我添加了jat,並在線程「main」中獲得了異常 org.apache.openjpa.persistence.PersistenceException隨着兩個ClassNotFoundException異常和一個RuntimeException – Killian

回答

1

嘗試添加公地集合jar文件到您的庫
Refer This Commons-collections

+0

嘗試過並得到: 線程「main」中的異常 org.apache.openjpa.persistence.PersistenceException有兩個ClassNotFoundExceptions和一個RuntimeException – Killian

+0

如果你碰巧使用Maven,添加爲依賴關係: 公地集合 公地集合 3.2.1 Anton

0

您需要將文件com.ibm.ws.jpa.thinclient.jar添加到類路徑中。如果你已經安裝了Websphere,你可以找到這個文件。例如: -

C:\IBM\SDP\runtimes\base_v7\runtimes\com.ibm.ws.jpa.thinclient_7.0.0.jar 

以另一種方式,如果你選擇JPA(EclipseLink)的參考實現,你不需要添加這個罐子。

+0

我嘗試這樣做,我得到這個錯誤: Exce ption在線程「main」中 org.apache.openjpa.persistence。PersistenceException 除了兩個ClassNotFoundException和一個RuntimeException – Killian

0

您必須添加jar文件,其中org.apache.derby.jdbc.EmbeddedDriver駐留,com.ibm.ws.jpa.thinclient_8.5.0.jar作爲依賴關係。

0

這太少太晚了?它看起來像你的「用戶」類沒有在persistence.xml中命名。您只有:

<class>jpa.Main</class> 

您是否嘗試將'jpa.Main'更改爲'jpa.User'?我不認爲你需要在persistence.xml中命名jpa.Main,只需要jpa.User。用戶是你的實體類。