tomcat中的PersistenceException(EntityManager没有持久性提供程序)

时间:2011-08-14 18:15:25

标签: java hibernate jpa

我正在尝试部署我在网上找到的a web app,它应该充当jpql shell webapp演示。 尝试在tomcat中部署并运行它,我创建了一个persistence.xml(在src目录中),如下所示

<?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="pu" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>jpql.Employee</class>
    <class>jpql.Job</class>
    <exclude-unlisted-classes>true</exclude-unlisted-classes>
    <properties>
      <property name="hibernate.connection.url" value="jdbc:postgresql://localhost/jpqldemodb"/>
      <property name="hibernate.connection.username" value="damon"/>
      <property name="hibernate.connection.password" value="damon"/>
      <property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
      <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
    </properties>
  </persistence-unit>
</persistence>

和EntityManager util(从其他站点借用)

package jpql.utils;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import org.apache.log4j.Logger;

public class EntityManagerUtil {
    static Logger logger = Logger.getLogger(EntityManagerUtil.class);
    private static EntityManager em=null;

    public static EntityManager getInstance() {
      if ( em == null) {
        EntityManagerFactory emf;
        emf = Persistence.createEntityManagerFactory("pu");
        if(emf == null){
            logger.info("emf is null");
        }else{
            logger.info("emf is created"+emf);
        }
        em = emf.createEntityManager();
        logger.info("em is created="+em);
        return em;
      }
      else
        return em;
    }
}

war文件结构是

META-INF
WEB-INF----------classes
                 lib-----------hibernate3.jar
                 web.xml     |-hibernate-jpa-2.0-1.00.Final.jar
                              --hibernate-tools.jar etc..

query.jsp

它成功部署,并且query.jsp作为欢迎页面出现。(在jsp中,调用类似于EntityManager em = jpql.utils.EntityManagerUtil.getInstance();发生在scriptlet内)。但是,当我点击时提交按钮,我收到此错误

org.apache.jasper.JasperException:在第25行处理JSP页面/query.jsp时发生异常

22:           if ( query == null || query.length() ==  0)
23:               return;
24: 
25:           EntityManager em = jpql.utils.EntityManagerUtil.getInstance();
26:           List lst = null;
27:           try {
28:             Query  qry = em.createQuery(query);


Stacktrace:
        org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:510)
        org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
        org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
        org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

root cause

javax.persistence.PersistenceException: No Persistence provider for EntityManager named pu
    javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
    javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:32)
    jpql.utils.EntityManagerUtil.getInstance(Unknown Source)
    org.apache.jsp.query_jsp._jspService(query_jsp.java:82)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

persistence.xml中提到的持久性提供程序是org.hibernate.ejb.HibernatePersistence,它位于hibernate3.jar。由于tomcat,它是否有错误?我正在使用tomcat 6.0.29

如果有人可以帮我解决这个问题,那就很好了

问候

戴蒙

更新 更新构建文件以正确构建war文件,正如Bozho所建议的那样

    ...
    <target name="makedirs">    
        <mkdir dir="${dir.build}"/>
        <mkdir dir="${dir.build}/META-INF"/>            
    </target>

    <target name="compile" depends="makedirs" description="Compile all source code">    
        <javac srcdir="${dir.src}" destdir="${dir.build}" includeAntRuntime="false" verbose="yes">    
            <classpath refid="compileclasspath"/>    
        </javac>                
        <copy file="${dir.src}/persistence.xml" todir="${dir.build}/META-INF"/>    
    ...    
</target>

    <target name="war" depends="compile">
        <war destfile="${dir.build}/${webapp.context.name}.war" webxml="${dir.resources}/web.xml">
        <classes dir="${dir.build}">                

        </classes>      
        ...
    </target>

1 个答案:

答案 0 :(得分:6)

正确的路径是WEB-INF/classes/META-INF/persistence.xml - 确保它在那里。