【问题标题】:Configuring JPA(Hibernate) with Tomcat 8.0使用 Tomcat 8.0 配置 JPA(休眠)
【发布时间】:2016-02-12 19:24:50
【问题描述】:

我在使用 Hibernate 作为 Eclipse 中的提供程序配置 Tomcat 8.0 和 JPA 时遇到问题。 当我尝试使用 DataSource Annotation 和标准 JDBC 方式连接到 DB 时,连接正常,但是当我尝试使用 Hibernate 时,我得到一个异常,即我的 PU 没有提供程序。我试图将休眠库移动到许多目录(web-inf/lib、apache TC 库),但我仍然得到同样的异常。我认为数据源配置正确。 下面我添加我的配置文件。

web.xml

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>HibernateTest</display-name> <resource-ref> <description>DB Connection</description> <res-ref-name>jdbc/HibernateTest</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>Faces Servlet</servlet-name> <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>/faces/*</url-pattern> </servlet-mapping> <context-param> <description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description> <param-name>javax.faces.STATE_SAVING_METHOD</param-name> <param-value>client</param-value> </context-param> <context-param> <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name> <param-value>resources.application</param-value> </context-param> <listener> <listener-class>com.sun.faces.config.ConfigureListener</listener-class> </listener> </web-app>

persistence.xml

<?xml version="1.0" encoding="UTF-8"?> <persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"> <persistence-unit name="HibernateTest" transaction-type="JTA"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <jta-data-source>jdbc/HibernateTest</jta-data-source> <properties> <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/> <property name="hibernate.hbm2ddl.auto" value="create-drop" /> </properties> </persistence-unit> </persistence> tomcat 中的 context.xml(仅片段)

<WatchedResource>WEB-INF/web.xml</WatchedResource> <Resource name="jdbc/HibernateTest" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000" username="admin" password="admin" driverClassName="org.postgresql.Driver" url="jdbc:postgresql://localhost:5432/Hibernate"/> <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>

public TestServlet() {
    super();
    // TODO Auto-generated constructor stub
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("HibernateTest");
    EntityManager em = emf.createEntityManager();

    em.close();
    emf.close();

}

} `

【问题讨论】:

  • 查看在context.xml 中使用java:comp/env/ 前缀jta-data-source 是否有帮助。

标签: java hibernate tomcat jpa provider


【解决方案1】:

由于 Tomcat 是一个 servlet 容器(与成熟的 J2EE 应用程序服务器相反),因此 JTA 不附带它。如果你想在 Tomcat 中使用 JTA,你必须加入你选择的实现。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-21
    • 2015-11-09
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 2015-08-07
    相关资源
    最近更新 更多