5
respostas

estou tentando conectar no postgresql mas nao estou conseguindo

co mo eu posso conectar no postgresql estou perdido, eu criei o banco, mas nao consigo acessar pela aplicação. o erro que aparece:

fev 06, 2019 8:38:10 AM org.hibernate.jpa.boot.internal.PersistenceXmlParser doResolve
INFO: HHH000318: Could not find any META-INF/persistence.xml file in the classpath
Exception in thread "main" java.lang.ExceptionInInitializerError
    at br.com.caelum.financas.teste.TesteConta.main(TesteConta.java:20)
Caused by: javax.persistence.PersistenceException: No Persistence provider for EntityManager named financas
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:85)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
    at br.com.caelum.financas.util.JPAUtil.<clinit>(JPAUtil.java:10)
    ... 1 more

se alguem puder me ajudar fico grato pq estou bem perdido

5 respostas

Bruno, pode postar seus códigos aqui? Sua classe com o main e o persistence.xml ?

Persistence.xml

<persistence xmlns="http://java.sun.com/xml/ns/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">

    <persistence-unit name="financas">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

<class>br.com.caelum.financas.modelo.Conta</class>
<properties>

<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/financas"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value=""/>

<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
</properties>
</persistence-unit>

    <persistence-unit name="financas-postgres">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>br.com.caelum.financas.modelo.Conta</class>
        <properties>
            <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost/financas" />
            <property name="javax.persistence.jdbc.user" value="postgres" />
            <property name="javax.persistence.jdbc.password" value="12345" />

            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
            <property name="hibernate.hbm2ddl.auto" value="update" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.format_sql" value="true" />
        </properties>
    </persistence-unit>

</persistence>

main teste conta

package br.com.caelum.financas.teste;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import br.com.caelum.financas.modelo.Conta;
import br.com.caelum.financas.util.JPAUtil;

public class TesteConta {
    public static void main(String[] args) {

        Conta conta = new Conta();
        conta.setTitular("titular");
        conta.setAgencia("123");
        conta.setBanco("Banco");
        conta.setNumero("456");


        EntityManager em = new JPAUtil().getEntityManager();

        em.getTransaction().begin();
        em.persist(conta);
        em.getTransaction().commit();



        em.close();


    }

}

Posta sua JPAUtil por favor?

package br.com.caelum.financas.util;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public class JPAUtil {


    private static EntityManagerFactory emf = Persistence.createEntityManagerFactory("financas");

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }


}

persistence-unit financas é do seu mysql