未找到JPA / HSQLDB持久性类错误

时间:2013-05-01 20:44:58

标签: java jpa persistence

我对JPA和HSQLDB很新,当我尝试创建我的EntityManagerFactory时,我发现了一个奇怪的错误。它是Eclipse中的Web Dynamic项目(Tomcat 6.0作为Web服务器),我导入了WEB-INF / lib中的所有库。我正在尝试在servlet中创建实体管理器工厂,但只是在行上给我一个类未找到的异常

emf = Persistence.createEntityManagerFactory("manager1");

这是我在servlet中使用的代码:

package view;

import java.io.IOException;
import javax.persistence.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.ThemeLivre;

public class AjouterTheme extends HttpServlet {
//private static final long serialVersionUID = 1L;

@PersistenceUnit(unitName ="DB")
private EntityManagerFactory emf; 

public AjouterTheme() {
    super();        
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    emf = Persistence.createEntityManagerFactory("DB");
    EntityManager em = emf.createEntityManager();
    String nomTheme = request.getParameter("nomtheme");
    String descTheme = request.getParameter("desctheme");
    EntityTransaction tx = em.getTransaction();
    tx.begin();
    ThemeLivre thml = new ThemeLivre(nomTheme, descTheme);
    em.persist(thml);
    tx.commit();


}

}

这是我的persistence.xml

<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="DB" transaction-type="RESOURCE_LOCAL">
 <provider>org.hibernate.ejb.HibernatePersistence</provider>
 <jta-data-source>java:/DefaultDS</jta-data-source>

<class>model.ThemeLivre</class>

<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.connection.url" value="jdbc:hsqldb:hsql://localhost/"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
<property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver"/>
<property name="javax.persistence.jdbc.user" value="sa"/>
     <property name="javax.persistence.jdbc.password" value=""/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.show_sql" value="true"/>

</properties>

请告诉我我做错了什么,或者我能做些什么。 谢谢

1 个答案:

答案 0 :(得分:0)

在您使用的servlet代码中

@PersistenceUnit(unitName ="DB")
private EntityManagerFactory emf; 

但你宣布

<persistence-unit name="manager1" transaction-type="RESOURCE_LOCAL">

更改一个或另一个持久性单元名称。

按照上面的输入

尝试添加hibernate持久性提供程序lib。您只添加规范(API)。需要实现本身(hibernate)来创建实体管理器。