0

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. enter image description here

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.

keepmoving
  • 1,813
  • 8
  • 34
  • 74
  • see http://stackoverflow.com/questions/1158159/no-persistence-provider-for-entitymanager-named – M Sach Oct 31 '15 at 14:38
  • this post too generic for me, there are so many possibilities and the accepted answer is not a my problem. One thing i have seen META-INF folder is not classes folder. it is equal to classes folder. Now can you please tell me what can i do in eclipse to add it into classes folder. silly question to ask but not sure how to add. – keepmoving Oct 31 '15 at 14:49
  • The persistence.xml must be inside a META-INF folder under your Java source directory, so that it ends up in the jar file, or in WEB-INF/classes, where it can be loaded by the ClassLoader. – JB Nizet Oct 31 '15 at 16:21
  • 1
    @JB Nizet, Thanks a lot.Now problem has been solved. – keepmoving Oct 31 '15 at 16:37

1 Answers1

0

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.