I have a project in Eclipse with an application that persists in the Postgresql 9.6 database through Hibernate, but Java is not recognizing the persistence-unit name that is set in persistence.xml , even this name by checking with the name passed to the "EntityManagerFactory" object.
Here is the exception log:
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named bd2_persistence_unit
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:61)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
at br.com.bd2.teste.TesteDatabase.main(TesteDatabase.java:15)
This is my main code:
package br.com.bd2.teste;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;
import br.com.bd2.exemplo.model.Fruteira;
public class TesteDatabase {
public static void main(String[] args) {
// Inicializacao da fabrica de objetos
// persistenceUnitName = "bd2", ou seja, relaciona com a conexao na
// persistence.xml (podera ter "n" unidades de persistencia)
EntityManagerFactory emf = Persistence.createEntityManagerFactory("bd2_persistence_unit");
EntityManager em = emf.createEntityManager();
// Os comandos a seguir devem ser executados "um por vez"
// Inserindo...
em.getTransaction().begin();
em.persist(new Fruteira("Fruteira"));
em.persist(new Fruteira("Fruteira 3"));
em.getTransaction().commit();
// Atualizando...
// Buscara a fruteira com id = 1. Observe o numero do id pelo pgAdmin3!
Fruteira fruteira = em.find(Fruteira.class, 1L);
if (fruteira != null) {
em.getTransaction().begin();
fruteira.setNome("Fruteira Legal");
em.merge(fruteira);
em.getTransaction().commit();
}
// Recuperando "n" objetos...
TypedQuery<Fruteira> q = em.createQuery("SELECT f " +
"FROM Fruteira f", Fruteira.class);
for (Fruteira each : q.getResultList()) {
System.out.println(each.toString());
}
// Excluindo...
// Buscara a fruteira com id = 1. Observe o numero do id pelo pgAdmin3!
fruteira = em.find(Fruteira.class, 1L);
if (fruteira != null) {
em.getTransaction().begin();
em.remove(fruteira);
em.getTransaction().commit();
}
// Recuperando "n" objetos...
q = em.createQuery("FROM Fruteira f", Fruteira.class);
for (Fruteira each : q.getResultList()) {
System.out.println(each.toString());
}
System.exit(0);
}
}
This is the organization for my project:
ThisismyHibernateconfigurationxml:
<?xmlversion="1.0" encoding="UTF-8"?>
<persistence 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"
version="2.1">
<persistence-unit name="bd2_persistence_unit">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>br.com.bd2.exemplo.model.Fruteira</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.connection.driver_class" value="org.postgresql.Driver" />
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
<property name="hibernate.connection.url" value="jdbc:postgresql://localhost/bd2" />
<property name="hibernate.connection.username" value="postgres" />
<property name="hibernate.connection.password" value="postgres" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="false" />
<property name="hibernate.archive.autodetection" value="true" />
</properties>
</persistence-unit>
</persistence>