简体   繁体   English

如何在Tomcat8中使用JPA

[英]How to use JPA with Tomcat8

0 and Tomcat8. 0和Tomcat8。 So I have created one web project in eclipse. 因此,我在Eclipse中创建了一个Web项目。 Firstly I have create one context.xml file and put it into META-INF folder inside web-content folder. 首先,我创建了一个context.xml文件,并将其放入web-content文件夹内的META-INF文件夹中。 It looks like this... 看起来像这样...

<context>
    <Resource name="jdbc/myDataSource" auth="Container" type="javax.sql.DataSource"
        maxActive="50" maxIdle="30" maxWait="10000" username="postgres"
        password="password" driverClassName="org.postgresql.Driver"
        url="jdbc:postgresql://localhost:5432/test" />
</Context>

Then create one entry into web.xml file. 然后在web.xml文件中创建一个条目。

<resource-ref>
        <description>postgres Datasource example</description>
        <res-ref-name>jdbc/myDataSource</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>

I want to create the EntityManagerFactory instance on application start so i also added one listener entry into web.xml file. 我想在应用程序启动时创建EntityManagerFactory实例,因此我还在web.xml文件中添加了一个侦听器条目。

<listener-class>com.listener.initlization.PersistenceListener</listener-class>

Now implement ServletContextListener to create EntityManagerFactory like this 现在实现ServletContextListener来创建EntityManagerFactory,就像这样

@Override
    public void contextInitialized(ServletContextEvent evt) {
        ServletContext ctxt = evt.getServletContext();

        entityManagerFactory = Persistence
                .createEntityManagerFactory("test");

    }

I am also putting one image which will tell you about the library and structure of them. 我还要放置一张图片,以告诉您有关它们的库和结构的信息。 在此处输入图片说明

now finally persistence.xml file looks like this. 现在终于persistence.xml文件看起来像这样。

<?xml version="1.0" encoding="UTF-8" ?>
<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_2_0.xsd"
    version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
    <persistence-unit name="test" transaction-type="RESOURCE_LOCAL">

    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <non-jta-data-source>java:comp/env/jdbc/myDataSource</non-jta-data-source>
<!--    <class>com.model.Employee</class> -->
        <properties>
            <!--   <property name="javax.persistence.jdbc.url" value="jdbc:postgres://localhost:5432/test" />
            <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
            <property name="javax.persistence.jdbc.user" value="postgres" />
            <property name="javax.persistence.jdbc.password" value="password" /> -->
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.format_sql" value="true" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
            <property name="hibernate.hbm2ddl.auto" value="update" />
            <property name="hibernate.connection.datasource" value="java:/comp/env/jdbc/myDataSource"/> 
        </properties>
    </persistence-unit>
</persistence>

So Now problem is when application starts and comes to Listener it throws an error. 所以现在的问题是,当应用程序启动并进入侦听器时,它将引发错误。

javax.persistence.PersistenceException: No Persistence provider for EntityManager named test.

Please let me know whats wrong in this program. 请让我知道该程序有什么问题。

I believe you are not using the context and resource-ref at all. 我相信您根本没有使用上下文和资源引用。 In this case, JPA uses only the information of the persistence.xml to connect the database. 在这种情况下,JPA仅使用persistence.xml的信息来连接数据库。 You are providing all necesary data there. 您在此处提供所有必需的数据。 Try seting wrong data in your context.xml to check this. 尝试在context.xml中设置错误的数据以进行检查。

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

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