在 Eclipse 中,我使用 DAO 制作了一个项目,它运行良好。然后我创建了一个新的 JPA 项目并将其添加到初始项目的部署程序集中。然后,我将一个 DAO 转换为与 JPA 一起使用,并将其放入 JPA 项目中。当我测试应用程序时,它给出了一个 Java 错误 NoClassDefFoundError(见底部)。我不知道为什么。我到处检查过,但找不到错误。JPA 在 servlet 中被调用:
package eshop;
....
import model.ProductModelDAO;
import model.ProductModelDAOImpl;
ProductModelDAO dao4 = new ProductModelDAOImpl("Product");
ArrayList products = dao4.getProductsByCategory(categoryId);
然后在 JPA 项目中,我有 Product.java,它是从“产品”表生成的。我有persistence.xml。我有两个原始的 DAO:
package model;
import java.util.*;
public interface ProductModelDAO {
public ArrayList searchProducts(String keyword);
public ArrayList getProductsByCategory(String categoryId);
public Product getProductById(String productID);
}
和:
package model;
import java.util.ArrayList;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.PersistenceContext;
public class ProductModelDAOImpl implements ProductModelDAO {
@PersistenceContext
private EntityManager em;
// this is the default constructor
public ProductModelDAOImpl()
{
this("Product");
}
public ProductModelDAOImpl(String unitName)
{
EntityManagerFactory emf = Persistence.createEntityManagerFactory(unitName);
em = emf.createEntityManager();
}
// this is the special constructor to set the entity manager.
// this is used when we run with EJB3
public ProductModelDAOImpl(EntityManager em)
{
this.em = em;
}
public void setEntityManager(EntityManager em)
{
this.em = em;
}
@Override
public ArrayList<Product> searchProducts(String keyword) {
@SuppressWarnings("unchecked")
ArrayList<Product> results = (ArrayList<Product>)
em.createQuery("select A from products A where (A.product_name = ?1 or A.descr = ?1)")
.setParameter(1, keyword)
.getResultList();
return results;
}
@Override
public ArrayList <Product> getProductsByCategory(String categoryId) {
@SuppressWarnings("unchecked")
ArrayList<Product> results = (ArrayList<Product>)
em.createQuery("select A from products A where A.category_id = ?1")
.setParameter(1, categoryId)
.getResultList();
return results;
}
@Override
public Product getProductById(String productID) {
Product results =
(Product) em.createQuery("select A from products A where A.product_id = ?1")
.setParameter(1, productID)
.getResultList();
return results;
}
}
持久性xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
xmlns="http://java.sun.com/xml/ns/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_1_0.xsd">
<persistence-unit name="abook">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<class>model.Product</class>
<properties>
<property name="openjpa.ConnectionDriverName" value="com.mysql.jdbc.Driver" />
<property name="openjpa.ConnectionURL" value="jdbc:mysql://localhost:3306/shop" />
<property name="openjpa.ConnectionUserName" value="root" />
<property name="openjpa.ConnectionPassword" value="" />
<property name="openjpa.Log" value="DefaultLevel=WARN, Tool=INFO" />
</properties>
</persistence-unit>
</persistence>
错误如下
java.lang.NoClassDefFoundError: model/ProductModelDAOImpl
at eshop.Servlet.doPost(Servlet.java:112)
at eshop.Servlet.doGet(Servlet.java:57)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)
at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:125)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:300)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:183)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.doIt(WebAppServletContext.java:3686)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3650)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:121)
at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2268)
at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2174)
at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1446)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:201)
at weblogic.work.ExecuteThread.run(ExecuteThread.java:173)