Erro no Hibernate

2 respostas
R

Estou tentando usar o Hibernate no Netbeans 6.5

Porém o programa retorna a segunte mensagem quando rodado:

init:

deps-module-jar:

deps-ear-jar:

deps-jar:

compile-single:

run-main:

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).

log4j:WARN Please initialize the log4j system properly.

java.lang.NullPointerException

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

at modelo.HibernateUtility.(HibernateUtility.java:21)

at modelo.Teste.main(Teste.java:28)

Exception in thread main java.lang.NullPointerException

at modelo.HibernateUtility.getSession(HibernateUtility.java:33)

at modelo.Teste.main(Teste.java:28)

Java Result: 1

BUILD SUCCESSFUL (total time: 0 seconds)

Esse é meu HibernateUtility:

package modelo;

import java.net.URL;

import javax.persistence.Entity;

import javax.persistence.EntityManager;

import org.apache.catalina.Manager;

import org.apache.jasper.tagplugins.jstl.core.Url;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.Configuration;

public class HibernateUtility {

private static SessionFactory factory;
static {

try {

URL url= Manager.class.getResource(“hibernate-config.xml”);

factory = new Configuration().configure().buildSessionFactory();

} catch (Exception e) {

e.printStackTrace();
factory = null;

}
}

public static Session getSession() {

return factory.openSession();

}

}

Essa minha classe de teste:

/*

  • To change this template, choose Tools | Templates
  • and open the template in the editor.
    */

package modelo;

import java.util.logging.Level;

import java.util.logging.Logger;

import javax.transaction.HeuristicMixedException;

import javax.transaction.HeuristicRollbackException;

import javax.transaction.RollbackException;

import javax.transaction.SystemException;

import javax.transaction.Transaction;

import org.hibernate.Session;

/**
*

  • @author rafaelbraga
    
    */
    
    public class Teste {
    
    public static void main(String Args[]) throws RollbackException{
    
    Session sessao = HibernateUtility.getSession();
     Transaction transaction = (Transaction) sessao.beginTransaction();
    
    Alunos alunos = new Alunos();
    
    alunos.setCpf("11111");
    alunos.setNome("11111");
    
     sessao.save(alunos);
     try {
         transaction.commit();
     } catch (HeuristicMixedException ex) {
         Logger.getLogger(Teste.class.getName()).log(Level.SEVERE, null, ex);
     } catch (HeuristicRollbackException ex) {
         Logger.getLogger(Teste.class.getName()).log(Level.SEVERE, null, ex);
     } catch (SecurityException ex) {
         Logger.getLogger(Teste.class.getName()).log(Level.SEVERE, null, ex);
     } catch (IllegalStateException ex) {
         Logger.getLogger(Teste.class.getName()).log(Level.SEVERE, null, ex);
     } catch (SystemException ex) {
         Logger.getLogger(Teste.class.getName()).log(Level.SEVERE, null, ex);
     }
    
     sessao.close();
    

    }

}

Obrigado desde já :smiley:

2 Respostas

R

Agora o erro que dá é esse:

init:

deps-module-jar:

deps-ear-jar:

deps-jar:

compile-single:

run-main:

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).

log4j:WARN Please initialize the log4j system properly.

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.Configuration.configure(Configuration.java:1414)

at modelo.HibernateUtility.(HibernateUtility.java:24)

at modelo.Teste.main(Teste.java:28)

Exception in thread main java.lang.NullPointerException

at modelo.HibernateUtility.getSession(HibernateUtility.java:35)

at modelo.Teste.main(Teste.java:28)

Java Result: 1

BUILD SUCCESSFUL (total time: 0 seconds)
R

Já resolvi o problema.
Valeu pela ajuda huahahahahaha
:slight_smile:

Criado 15 de janeiro de 2009
Ultima resposta 28 de jan. de 2009
Respostas 2
Participantes 1