Problemas ao conectar usando postgres e Hibernate

6 respostas
brunoaquino

Mal comecei em Java e já tenho que fazer meu TCC e arrisquei na linguagem, mas começo a ter problemas aqui em casa :)

Estou criando uma aplicação Desktop padrão MVC usando NetBeans com Postgres 8.4

Como estou iniciando e meu projetos meio que saem seguindo como "receita de bolo" levei para meu professor na faculdade essa semana, e lá ele consegui conectar e rodar o projeto normalmente usando o JAVA DB, mas em casa em não consigo o mesmo usando o Postgres.

Estou usando as seguintes bibliotecas:

Hibernate, Hibernate JPA e os .JAR ejb3-persistence e postgresql-8.4-701.jdbc4

alguém pode me ajudar?

Classe Dao:

package dao;

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

/**
 *
 * @author Bruno Aquino
 */
public class Dao {
    private static EntityManagerFactory emf = null;
    private static EntityManager em = null;

    private static EntityManager getEm(){
        if(em == null){
            try{
                emf = Persistence.createEntityManagerFactory("BancoTestePU");
                //instancia um objeto EntityManager
                em = emf.createEntityManager();
            }catch (Exception e){
                System.out.println(e.getMessage());
            }
        }
        return em;
    }

Persistence

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" 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_1_0.xsd">
  <persistence-unit name="BancoTestePU" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>modelo.Aluno</class>
    <class>modelo.Cidade</class>
    <properties>
      <property name="hibernate.connection.username" value="postgres"/>
      <property name="hibernate.connection.driver_class" value="org.apache.derby.jdbc.ClientDriver"/>
      <property name="hibernate.connection.password" value="metallica"/>
      <property name="hibernate.connection.url" value="jdbc:derby://localhost:1527/BancoTeste"/>
      <property name="hibernate.hbm2ddl.auto" value="update"/>
    </properties>
  </persistence-unit>
</persistence>

Resolvido

6 Respostas

V

Qual o erro que está dando ?

thiago.sydow

você não alterou todo o xml… ele tá fazendo referência ao derby ainda

<properties>  
      <property name="hibernate.connection.username" value="postgres"/>  
      <property name="hibernate.connection.driver_class" value="org.apache.derby.jdbc.ClientDriver"/>  
      <property name="hibernate.connection.password" value="metallica"/>  
      <property name="hibernate.connection.url" value="jdbc:derby://localhost:1527/BancoTeste"/>  
      <property name="hibernate.hbm2ddl.auto" value="update"/>  
    </properties>

mude para

<properties>  
      <property name="hibernate.connection.username" value="postgres"/>  
      <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />  
      <property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>  
      <property name="hibernate.connection.password" value="metallica"/>  
      <property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/BancoTeste"/>  
      <property name="hibernate.hbm2ddl.auto" value="update"/>  
    </properties>
brunoaquino

Saída que está gerando é essa:

17/06/2010 11:12:29 org.hibernate.cfg.annotations.Version
INFO: Hibernate Annotations 3.3.1.GA
17/06/2010 11:12:29 org.hibernate.cfg.Environment
INFO: Hibernate 3.2.5
17/06/2010 11:12:29 org.hibernate.cfg.Environment
INFO: hibernate.properties not found
17/06/2010 11:12:29 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: Bytecode provider name : cglib
17/06/2010 11:12:29 org.hibernate.cfg.Environment
INFO: using JDK 1.4 java.sql.Timestamp handling
17/06/2010 11:12:29 org.hibernate.ejb.Version
INFO: Hibernate EntityManager 3.3.2.GA
17/06/2010 11:12:30 org.hibernate.cfg.AnnotationBinder bindClass
INFO: Binding entity from annotated class: modelo.Aluno
17/06/2010 11:12:31 org.hibernate.cfg.annotations.EntityBinder bindTable
INFO: Bind entity modelo.Aluno on table Aluno
17/06/2010 11:12:31 org.hibernate.cfg.AnnotationBinder bindClass
INFO: Binding entity from annotated class: modelo.Cidade
17/06/2010 11:12:31 org.hibernate.cfg.annotations.EntityBinder bindTable
INFO: Bind entity modelo.Cidade on table Cidade
17/06/2010 11:12:31 org.hibernate.cfg.AnnotationConfiguration secondPassCompile
INFO: Hibernate Validator not found: ignoring
17/06/2010 11:12:31 org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: Using Hibernate built-in connection pool (not for production use!)
17/06/2010 11:12:31 org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: Hibernate connection pool size: 20
17/06/2010 11:12:31 org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: autocommit mode: true
17/06/2010 11:12:31 org.hibernate.connection.DriverManagerConnectionProvider configure
SEVERE: JDBC Driver class not found: org.apache.derby.jdbc.ClientDriver

thiago.sydow

Como eu disse, não tá achando o driver, pois está configurado para o derby, faça a alteração que te passei.

Att,

brunoaquino

Valeu pessoal :smiley:

Problema resolvido!

Realmente não tinha alterado todo o persistence!

Muito bom o forum, meu primeiro tópico e foi respondido rápidamente.

Abraços!

thiago.sydow

Ótimo, só edite o tópico e coloque [Resolvido] no final dele.

Att,

Criado 17 de junho de 2010
Ultima resposta 17 de jun. de 2010
Respostas 6
Participantes 3