java.lang.NoClassDefFoundError JPA示例

时间:2013-08-07 18:34:34

标签: java jpa derby

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

4 个答案:

答案 0 :(得分:1)

尝试将commons-collections jar文件添加到您的库中 Refer This Commons-collections

答案 1 :(得分:0)

您需要将文件com.ibm.ws.jpa.thinclient.jar添加到类路径中。如果您已安装Websphere,则可以找到此文件。 e.g:

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

另一方面,如果您选择JPA的参考实现(EclipseLink),则不需要添加此jar。

答案 2 :(得分:0)

您必须在org.apache.derby.jdbc.EmbeddedDriver所在的位置添加jar文件,并将com.ibm.ws.jpa.thinclient_8.5.0.jar添加为依赖项。

答案 3 :(得分:0)

这太晚了吗?看起来你的“User”类没有在persistence.xml中命名。你刚才:

g++ -std=c++14 -O2 -Wall -pedantic -pthread main.cpp

您是否尝试将'jpa.Main'更改为'jpa.User'?我认为你不需要在persistence.xml中命名jpa.Main,只需要jpa.User。用户是您的实体类。