简体   繁体   中英

How to use JPA with Tomcat8

0 and Tomcat8. So I have created one web project in eclipse. Firstly I have create one context.xml file and put it into META-INF folder inside web-content folder. 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.

<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.

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

Now implement ServletContextListener to create EntityManagerFactory like this

@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.

<?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. You are providing all necesary data there. Try seting wrong data in your context.xml to check this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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