Solucionado (ver solução)
Solucionado
(ver solução)
3
respostas

Hibernate não conecta ao banco Oracle

Estou no cap 2 e não estou conseguindo realizar os primeiros inserts no banco Oracle. O mesmo não se encontra na minha maquina. Estou recebendo o seguinte erro:

WARN: HHH000342: Could not obtain connection to query metadata : Unknown host specified Exception in thread "main" javax.persistence.PersistenceException: [PersistenceUnit: controleFinancas] Unable to build EntityManagerFactory at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:915) at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:890) at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:57) at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:63) at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47) at br.com.caelum.financas.teste.TestaConta.main(TestaConta.java:19) Caused by: org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' not set at org.hibernate.service.jdbc.dialect.internal.DialectFactoryImpl.determineDialect(DialectFactoryImpl.java:97) at org.hibernate.service.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:67) at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:170) at org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:75) at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:159) at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:131) at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:77) at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2283) at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2279) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1748) at org.hibernate.ejb.EntityManagerFactoryImpl.(EntityManagerFactoryImpl.java:94) at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:905) ... 5 more

Poderiam ajudar?

Persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- example of a default 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_1_0.xsd"
             version="1.0">
    <persistence-unit name="controleFinancas">
        <class>br.com.financas.modelo.Conta</class>
        <properties>
            <property name="dialect" value="org.hibernate.dialect.OracleDialect"/> 
            <property name="connection.driver_class" value="oracle.jdbc.driver.OracleDriver"/>
            <property name="hibernate.connection.username" value="go"/>
            <property name="hibernate.connection.password" value="teste"/>
            <property name="hibernate.connection.url" value="jdbc:oracle:thin:@abc0100:1521/teste"/> 
            <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>

Conta.java

package br.com.financas.modelo;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;

@Entity
@SequenceGenerator(name = "SEQ_CONTAS", sequenceName = "SEQ_CONTAS", initialValue = 1)
public class Conta {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_CONTAS")
    private Integer id;
    private String titular;
    private String banco;
    private String agencia;
    private String numero;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getTitular() {
        return titular;
    }
    public void setTitular(String titular) {
        this.titular = titular;
    }
    public String getBanco() {
        return banco;
    }
    public void setBanco(String banco) {
        this.banco = banco;
    }
    public String getAgencia() {
        return agencia;
    }
    public void setAgencia(String agencia) {
        this.agencia = agencia;
    }
    public String getNumero() {
        return numero;
    }
    public void setNumero(String numero) {
        this.numero = numero;
    }


}

TestaConta.java

package br.com.caelum.financas.teste;

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


import br.com.financas.modelo.Conta;

public class TestaConta {

    public static void main(String[] args) {
        Conta conta = new Conta();
        conta.setTitular("Giovanni");
        conta.setBanco("Santander");
        conta.setAgencia("123");
        conta.setNumero("456");

        EntityManagerFactory emf = Persistence.createEntityManagerFactory("controleFinancas");
        EntityManager em = emf.createEntityManager();

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

        em.close();
        emf.close();

    }

}
3 respostas

Faz um teste troca essa linha.

 <property name="dialect" value="org.hibernate.dialect.OracleDialect"/>

<property name="connection.driver_class" value="oracle.jdbc.driver.OracleDriver"/>

ficando assim.

 <property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect"/>

<property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver"/>

Continuou o mesmo erro :(

solução!

Consegui arrumar, na parte da URL de conexão o SID estava depois de uma " / " , alterai para " : " e consegui.

Antes:

            <property name="hibernate.connection.url" value="jdbc:oracle:thin:@abc0100:1521/teste"/>

Depois:

            <property name="hibernate.connection.url" value="jdbc:oracle:thin:@abc0100:1521:teste"/>