Duvida com hibernate

3 respostas
R

Olá,

alguem sabe o pq deste erro quando uso hibernate??

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

log4j:WARN Please initialize the log4j system properly.

net.sf.hibernate.MappingException: Error reading resource: hibernate/Amigo.hbm.xml

at net.sf.hibernate.cfg.Configuration.addClass(Configuration.java:336)

at hibernate.AmigoDAO.(AmigoDAO.java:18)

at hibernate.TesteAmigo.main(TesteAmigo.java:31)

Caused by: net.sf.hibernate.MappingException: org.dom4j.DocumentException: <a href="http://hibernate.sourceforge.net">hibernate.sourceforge.net</a> Nested exception: <a href="http://hibernate.sourceforge.net">hibernate.sourceforge.net</a>

at net.sf.hibernate.cfg.Configuration.addInputStream(Configuration.java:293)

at net.sf.hibernate.cfg.Configuration.addClass(Configuration.java:333)

… 2 more

Caused by: org.dom4j.DocumentException: <a href="http://hibernate.sourceforge.net">hibernate.sourceforge.net</a> Nested exception: <a href="http://hibernate.sourceforge.net">hibernate.sourceforge.net</a>

at org.dom4j.io.SAXReader.read(SAXReader.java:358)

at net.sf.hibernate.cfg.Configuration.addInputStream(Configuration.java:283)

… 3 more

Oque eu fiz de errado?

Obrigado!

3 Respostas

Og1

net.sf.hibernate.MappingException: Error reading resource: hibernate/Amigo.hbm.xml

Verifica se seu mapeamento está mesmo nesse pacote… o Hibernate nem conseguiu ler ele.

vamos ver se resolve :wink:

R

Oi,

como sou iniciante não sei exatamente verificar isso... mas acho que está correto defini o package corretamente em cada classe... veja meu código:

package hibernate;

public class Amigo {
   private String nome;
   private String endereco;
   private String telefone;
   private String celular;
   private String email;
   private java.util.Date nascimento;

   public Amigo() {
   }
    
   public String getNome(){
      return nome;
   }
    
   public void setNome(String nome){
      this.nome = nome;
   }
    
   public String getEndereco(){
      return endereco;
   }
    
   public void setEndereco(String endereco){
      this.endereco = endereco;
   }
    
   public String getTelefone(){
      return telefone;
   }
    
   public void setTelefone(String telefone){
      this.telefone = telefone;
   }
    
   public String getCelular(){
      return celular;
   }
    
   public void setCelular(String celular){
      this.celular = celular;
   }
    
   public String getEmail(){
      return email;
   }
    
   public void setEmail(String email){
      this.email = email;
   }

   public java.util.Date getNascimento(){
      return nascimento;
   }
    
   public void setNascimento(java.util.Date nascimento){
      this.nascimento = nascimento;
   }

}
package hibernate;

import java.util.List;
import net.sf.hibernate.*;
import net.sf.hibernate.cfg.Configuration;

public class AmigoDAO{

   private SessionFactory factory;

   public AmigoDAO() throws Exception{
       factory = new Configuration().addClass(Amigo.class).buildSessionFactory();
   }     

   public void insert(Amigo amigo) throws Exception{
      Session session = factory.openSession();
      session.save(amigo);
      session.flush();
      session.close();
   }
    
   public java.util.List getList(String condicao) throws Exception{
      Session session = factory.openSession();
      List amigos = session.find(condicao);
      session.flush();
      session.close();
      return amigos;
   }
    
   public Amigo retrieve(String pk) throws Exception{
      Session session = factory.openSession();
      Amigo amigo = (Amigo)session.load(Amigo.class, pk);
      session.flush();
      session.close();
      return amigo;
   }
    
   public void delete(Amigo amigo) throws Exception{
      Session session = factory.openSession();
      session.delete(amigo);
      session.flush();
      session.close();
   }
}
package hibernate;

/**
 *
 * @author Rodrigo
 */
public class TesteAmigo { 

   public static void main(String[] args) throws Exception { 

     try 
     { 
       Amigo amigo = new Amigo(); 
       amigo.setNome("Angelica"); 
      // amigo.setEndereco("seu endereco"); 
      // amigo.setTelefone("seu fone"); 
      // amigo.setCelular("seu celular"); 
      // amigo.setEmail("seu mail"); 
       //amigo.setNascimento("data do tipo Date"); 
         
       AmigoDAO dao = new AmigoDAO(); 
       dao.insert(amigo); 
 System.out.println("dados persistidos");

     } 
     catch(Exception e) 
     {    
       e.printStackTrace();//aqui vc vai saber que xabu é esse. 
     } 
   } 
}
Amigo.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping.dtd">
<hibernate-mapping>
    <class name="Amigo" table="amigo">
        <id name="nome" column="nome" type="string">
            <generator class="assigned"/>
        </id>
        <property name="endereco" type="string"/>
        <property name="telefone" column="fone" type="string"/>
        <property name="celular" column="cel" type="string"/>
        <property name="email" type="string"/>
        <property name="nascimento" type="date"/>
    </class>
</hibernate-mapping>
hibernate.properties
hibernate.dialect=net.sf.hibernate.dialect.MySQLDialect
hibernate.connection.driver_class = com.mysql.jdbc.Driver
hibernate.connection.url = jdbc:mysql://localhost:3306/test
hibernate.connection.username = root
hibernate.connection.password =
log4j.properties
log4j.rootLogger=DEBUG, dest1 

log4j.appender.dest1=org.apache.log4j.ConsoleAppender 
log4j.appender.dest1.layout=org.apache.log4j.PatternLayout 
log4j.appender.dest1.layout.ConversionPattern=%d %-5p %-5c{3} %x -> %m%n

Obrigado!

RobsonFagundes

è realmente o hibernate nao esta encontrando o Amigo.hbm.xml

net.sf.hibernate.MappingException: Error reading resource: hibernate/Amigo.hbm.xml

tente o seguinte faça o build da sua aplicação e coloque o Amigo.hbm.xml
junto com os .class , por ex: C:\tmp\rafsantosESCOLA\build\classes

Criado 2 de outubro de 2006
Ultima resposta 3 de out. de 2006
Respostas 3
Participantes 3