所以我想通過一些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();
}
}
您確定commons-collections jar在運行時可用於容器嗎? – chrylis
您使用的是JPA實現?基於Apache OpenJPA的WebSphere JPA解決方案? –
@chrylis我添加了jat,並在線程「main」中獲得了異常 org.apache.openjpa.persistence.PersistenceException隨着兩個ClassNotFoundException異常和一個RuntimeException –
Killian