所以我正在尝试处理一些 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>
我的用户.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();
}
}