我試圖部署a web app我淨,這應該作爲一個JPQL殼體的webapp演示找到。 嘗試部署和Tomcat的運行它,我創建了一個persistence.xml
(在src目錄下),如下的PersistenceException(無持久性提供的EntityManager)在Tomcat中
<?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;
}
}
戰爭文件結構
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.Ent ityManagerUtil.getInstance();發生一個scriptlet內)。然而,當我點擊提交按鈕,我收到此錯誤
org.apache.jasper.JasperException:發生異常處理JSP頁面/query.jsp 25行
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
如果有人能幫助我解決這個問題,這將是很好
個問候
達蒙
更新: 更新,以建立正確的戰爭文件,如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>
這是'WEB-INF /班/ META-INF/persistence.xml' – axtavt
感謝指教..但我找不到在戰爭任務的元素,包括在WEB-INF/META-INF xml文件。我嘗試了webinf和metainf元素,但他們把xml文件放到了web-inf或meta-inf目錄中。原因是我的無知.. –
@axtavt謝謝..我應該複製粘貼它,減少技術錯誤的概率 – Bozho