Ajuda com Hibernate

4 respostas
rafaelctork

Estou com um problema com o hibernate … vo postar o erro logo abaixo… se puder ajudar… agradeço…

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).
log4j:WARN Please initialize the log4j system properly.

Caused by: org.hibernate.HibernateException: hibernate.cfg.xml not found

at org.hibernate.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:147)

at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:1405)

at org.hibernate.cfg.Configuration.configure(Configuration.java:1427)

at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:972)

O arquivo hibernate.cfg.xml ja criei… mas nao ta encontrando nao sei pq.

4 Respostas

H

Onde você colocou o arquivo “hibernate.cfg.xml” ?
E como você está criando a sua SessionFactory?

rafaelctork

A classe hibernate.cfg.xml esta no pacote “pacote padrao” junto com o Log4j.properties.

minha classe de conexão com o banco esta assim…

package br.com.DAO;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

public class HibernateUtilAnnotation {

    private static final SessionFactory sessionFactory;
    private static final ThreadLocal<Session> threadlocal = new ThreadLocal<Session>();
    

    static {
        try {
            sessionFactory = new AnnotationConfiguration().
                    configure("hibernate.cfg.xml").buildSessionFactory();
        } catch (Throwable t) {
            throw new ExceptionInInitializerError(t);
        }

    }

    public static Session getInstance() {
        Session sessao = (Session) threadlocal.get();
        sessao = sessionFactory.openSession();
        threadlocal.set(sessao);
        return sessao;
    }
}

e eu estou criando a conexão assim

public class ClienteDAO {

    Session sessao = HibernateUtilAnnotation.getInstance();
    Transaction tx = null;


    public void inserir(Cliente c) throws SQLException {
        if (c != null) {
            try {
                tx = sessao.beginTransaction();
                sessao.save(c);
                tx.commit();
                JOptionPane.showMessageDialog(null, "Cliente Cadastrado!");

            } catch (HibernateException e) {
                e.printStackTrace();
                tx.rollback();
            } finally {
                sessao.close();
            }

        }//fim do if

    }//fim do metodo inserir.......
}

A Classe hibernate.cfg.xml esta dessa forma…

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD
3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
    <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
    <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/ProjetoTKT</property>
    <property name="hibernate.connection.username">postgres</property>
    <property name="hibernate.connection.password">1234</property>

    <mapping class="br.com.model.Cliente"/>
    
  </session-factory>
</hibernate-configuration>

Agradeço desde jah…

H

Altere o seu HibernateUtil para:

public class HibernateUtil {

	private static final SessionFactory factory;
	static {
		try {
			// Create the SessionFactory from hibernate.cfg.xml
			Configuration conf = new AnnotationConfiguration();
			conf.configure();
			factory = conf.buildSessionFactory();
		} catch (Throwable ex) {
			// Make sure you log the exception, as it might be swallowed
			System.err.println("Initial SessionFactory creation failed." + ex);
			throw new ExceptionInInitializerError(ex);
		}
	}

    public static Session getSession() {	
        return factory.openSession();
    }
}

Utilização:

public void salvar(Object object) {
    Session session = HibernateUtil.getSession();
	Transaction transaction = session.beginTransaction();
	try {
           session.save(object);
	   transaction.commit();
	} catch (Exception e) {
		e.printStackTrace();
	        rollback();			
	}		
}

Espero ter ajudado.

rafaelctork

Revisei as bibliotecas do projeto e sumiu os erros que estava dando anteriormente, mas apareceu um novo erro…

Initial SessionFactory creation failed.java.lang.NoClassDefFoundError: org/hibernate/annotations/common/reflection/ReflectionManager
Exception in thread “AWT-EventQueue-0” java.lang.ExceptionInInitializerError

acredito que seja erro de bilbioteca tbm…

Criado 17 de fevereiro de 2010
Ultima resposta 18 de fev. de 2010
Respostas 4
Participantes 2