Question

I receive the following error in my standalone java application:

Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named DataSourcePostgres

Here's my 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://www.oracle.com/webfolder/technetwork/jsc/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="DataSourcePostgres">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <class>Denarnica</class>
    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.ProgressDialect"/>
        <property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
        <!--<property name="hibernate.show_sql" value="true"/>-->
        <property name="hibernate.connection.username" value="*****"/>
        <property name="hibernate.connection.password" value="*******"/>
        <property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/postgres"/>
        <property name="hibernate.hbm2ddl.auto" value="update"/>
    </properties>
</persistence-unit>
</persistence>

and here's my Server.java:

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import java.util.List;

public class Server {

    private static final String PERSISTENCE_UNIT_NAME = "DataSourcePostgres";
    private static EntityManagerFactory factory;

    public static void main(String[] args) {
        factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
        EntityManager em = factory.createEntityManager();
        // Read the existing entries and write to console
        Query q = em.createQuery("select d from Data d");
        List<Denarnica> dataList= q.getResultList();
        System.out.println("Size: " + dataList.size());
    }
}

I've been looking at some similar problems, but the solutions people suggested to them don't seem to help me. Any advice?

Was it helpful?

Solution

My problem was I didn't put the persistence.xml into a META-INF folder. After I fixed that everything worked.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top