简体   繁体   English

使用Tomcat 8.0配置JPA(休眠)

[英]Configuring JPA(Hibernate) with Tomcat 8.0

I have problem with configuring Tomcat 8.0 with JPA using Hibernate as a provider in Eclipse. 我在使用Hibernate作为Eclipse中的提供程序的JPA配置Tomcat 8.0时遇到问题。 When i tried to use DataSource Annotation and standard JDBC way to connect to DB, the connection is ok but when i try to use Hibernate and i get an exception about there is no provider for my PU. 当我尝试使用DataSource Annotation和标准JDBC方式连接到DB时,连接就可以了,但是当我尝试使用Hibernate时,却遇到了关于我的PU没有提供程序的异常。 I tried to move hibernate libs to many directories (web-inf/lib, apache TC library) but i still get the same exception. 我试图将休眠库移动到许多目录(web-inf / lib,Apache TC库),但是我仍然遇到相同的异常。 I think the data source is configured properly. 我认为数据源配置正确。 Below i add my configured files. 在下面,我添加我的配置文件。

web.xml 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 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> context.xml in tomcat(only fragment) <?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中的<?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> 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();

}

} ` }`

Since Tomcat is a servlet container (as opposed to a full blown J2EE application server), JTA doesn't come with it. 由于Tomcat是一个Servlet容器(与功能完善的J2EE应用服务器相对),因此JTA并未随附。 If you want to use JTA in Tomcat, you have to drop in an implementation of your choice. 如果要在Tomcat中使用JTA,则必须加入自己选择的实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM