Erro com Hibernate

3 respostas
Luciana_GF

Olá Pessoal,

Rescentemente comecei s estudar hibernate, no próprio fórum encontrei conteúdos muito bom para quem esta iniciando, vi no link

o documento em pdf, por sinal ótimo:<a href="//www.guj.com.br/java.tutorial.artigo.174.1.guj">http://www.guj.com.br/java.tutorial.artigo.174.1.guj</a> . Assim tentei reproduzir como esta no documento,

porém esta ocorrendo erros, segue abaixo:

run:

24/09/2010 13:41:54 org.hibernate.cfg.Environment 

INFO: Hibernate 3.2.5

24/09/2010 13:41:54 org.hibernate.cfg.Environment 

INFO: hibernate.properties not found

24/09/2010 13:41:54 org.hibernate.cfg.Environment buildBytecodeProvider

INFO: Bytecode provider name : cglib

24/09/2010 13:41:54 org.hibernate.cfg.Environment 

INFO: using JDK 1.4 java.sql.Timestamp handling

24/09/2010 13:41:54 org.hibernate.cfg.Configuration configure

INFO: configuring from resource: hibernate.cfg.xml

24/09/2010 13:41:54 org.hibernate.cfg.Configuration getConfigurationInputStream

INFO: Configuration resource: hibernate.cfg.xml

24/09/2010 13:41:54 org.hibernate.cfg.Configuration addResource

INFO: Reading mappings from resource : Turma.hbm.xml

24/09/2010 13:41:54 org.hibernate.cfg.HbmBinder bindRootPersistentClassCommonValues

INFO: Mapping class: Turma -> Turma

24/09/2010 13:41:55 org.hibernate.cfg.HbmBinder bindCollection

INFO: Mapping collection: Turma.alunos -> Turma_has_Aluno

24/09/2010 13:41:55 org.hibernate.cfg.Configuration addResource

INFO: Reading mappings from resource : Pessoa.hbm.xml

24/09/2010 13:41:55 org.hibernate.cfg.HbmBinder bindRootPersistentClassCommonValues

INFO: Mapping class: Pessoa -> Pessoa

24/09/2010 13:41:55 org.hibernate.cfg.Configuration addResource

INFO: Reading mappings from resource : Aluno.hbm.xml

24/09/2010 13:41:55 org.hibernate.cfg.HbmBinder bindJoinedSubclass

INFO: Mapping joined-subclass: Aluno -> Aluno

24/09/2010 13:41:55 org.hibernate.cfg.HbmBinder bindCollection

INFO: Mapping collection: Aluno.turmas -> Turma_has_Aluno

24/09/2010 13:41:55 org.hibernate.cfg.Configuration addResource

INFO: Reading mappings from resource : Professor.hbm.xml

24/09/2010 13:41:55 org.hibernate.cfg.HbmBinder bindJoinedSubclass

INFO: Mapping joined-subclass: Professor -> Professor

24/09/2010 13:41:55 org.hibernate.cfg.Configuration addResource

INFO: Reading mappings from resource : Endereco.hbm.xml

24/09/2010 13:41:55 org.hibernate.cfg.HbmBinder bindRootPersistentClassCommonValues

INFO: Mapping class: Endereco -> Endereco

24/09/2010 13:41:55 org.hibernate.cfg.Configuration doConfigure

INFO: Configured SessionFactory: null

24/09/2010 13:41:55 org.hibernate.cfg.HbmBinder bindCollectionSecondPass

INFO: Mapping collection: Professor.turmas -> Turma

org.hibernate.MappingException: An association from the table Turma refers to an unmapped class: Disciplina

at org.hibernate.cfg.Configuration.secondPassCompileForeignKeys(Configuration.java:1252)

at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1170)

at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1286)

Estou usando netbeans e postgres,

:thumbup:Se puderem me ajudar desde já agradeço, fui —>

3 Respostas

oddy.silva

Olá, tudo bem?
Você não mapeou a classe Disciplina.

...unmapped class: Disciplina

Se você olhar no seu banco, verá que esta tabela não foi criada.

Se você estiver usando o Hibernate com as Annotations, em algum lugar você deve ter criado uma classe que mapeia as classes pras tabelas (GeraTabelas talvez), só que você esqueceu de adicionar esta classe, ou seja, esquecer de mapeá-la. Lembre-se de anotar a classe Disciplina com @Entity.

Se estiver usando apenas XML, então dê uma olhada na maneira em que você mapeou as outras classes e verifique se foi feito o mesmo para a classe Discliplina.

Flw.

Luciana_GF

Vou tratar de fazer isso agora!!!

Valeu, até mais !!! :idea:

Luciana_GF

Olá Pessoal, Novos problemas a vista!!!

Refiz novamente tudo classes e mapeamentos, mas está difícil, novos erros cadas vez  mais diferentes aparecem o último é:

run:

07/10/2010 13:34:22 org.hibernate.cfg.Environment 

INFO: Hibernate 3.2.5

07/10/2010 13:34:22 org.hibernate.cfg.Environment 

INFO: hibernate.properties not found

07/10/2010 13:34:22 org.hibernate.cfg.Environment buildBytecodeProvider

INFO: Bytecode provider name : cglib

07/10/2010 13:34:22 org.hibernate.cfg.Environment 

INFO: using JDK 1.4 java.sql.Timestamp handling

07/10/2010 13:34:22 org.hibernate.cfg.Configuration configure

INFO: configuring from resource: hibernate.cfg.xml

07/10/2010 13:34:22 org.hibernate.cfg.Configuration getConfigurationInputStream

INFO: Configuration resource: hibernate.cfg.xml

07/10/2010 13:34:22 org.hibernate.cfg.Configuration addResource

INFO: Reading mappings from resource : Turma.hbm.xml

07/10/2010 13:34:22 org.hibernate.cfg.HbmBinder bindRootPersistentClassCommonValues

INFO: Mapping class: Disciplina -> Disciplina

07/10/2010 13:34:22 org.hibernate.cfg.HbmBinder bindCollection

INFO: Mapping collection: Disciplina.alunos -> Turma_has_Aluno

07/10/2010 13:34:22 org.hibernate.cfg.Configuration addResource

INFO: Reading mappings from resource : Pessoa.hbm.xml

07/10/2010 13:34:22 org.hibernate.cfg.HbmBinder bindRootPersistentClassCommonValues

INFO: Mapping class: Pessoa -> Pessoa

07/10/2010 13:34:22 org.hibernate.cfg.Configuration addResource

INFO: Reading mappings from resource : Aluno.hbm.xml

07/10/2010 13:34:22 org.hibernate.cfg.HbmBinder bindJoinedSubclass

INFO: Mapping joined-subclass: Aluno -> Aluno

07/10/2010 13:34:22 org.hibernate.cfg.HbmBinder bindCollection

INFO: Mapping collection: Aluno.turmas -> Turma_has_Aluno

07/10/2010 13:34:22 org.hibernate.cfg.Configuration addResource

INFO: Reading mappings from resource : Professor.hbm.xml

07/10/2010 13:34:22 org.hibernate.cfg.HbmBinder bindJoinedSubclass

INFO: Mapping joined-subclass: Professor -> Professor

07/10/2010 13:34:22 org.hibernate.cfg.Configuration addResource

INFO: Reading mappings from resource : Endereco.hbm.xml

07/10/2010 13:34:22 org.hibernate.cfg.HbmBinder bindRootPersistentClassCommonValues

INFO: Mapping class: Endereco -> Endereco

07/10/2010 13:34:22 org.hibernate.cfg.Configuration doConfigure

INFO: Configured SessionFactory: null

org.hibernate.MappingException: Association references unmapped class: Turma

at org.hibernate.cfg.HbmBinder.bindCollectionSecondPass(HbmBinder.java:2380)

at org.hibernate.cfg.HbmBinder$CollectionSecondPass.secondPass(HbmBinder.java:2662)

at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:43)

at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1130)

at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1286)

at HibernateUtility.(HibernateUtility.java:22)

at Teste.main(Teste.java:22)

Exception in thread “main” java.lang.NullPointerException

at HibernateUtility.getSessionFactory(HibernateUtility.java:30)

at Teste.main(Teste.java:22)

Java Result: 1

CONSTRUÍDO COM SUCESSO (tempo total: 0 segundos)
classes citadas acimas são:

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.Configuration;

public class HibernateUtility {

private static SessionFactory factory;

static {

try {

factory = new Configuration().configure(hibernate.cfg.xml).buildSessionFactory();

} catch (Exception e) {

e.printStackTrace();

factory = null;

}

}

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

}

public class Teste {

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

Session sessao = HibernateUtility.getSessionFactory(); //Abrindo uma sessão

org.hibernate.Transaction transaction = sessao.beginTransaction(); //Iniciando uma transação

Aluno aluno = new Aluno(); //Instanciando um objeto transiente

aluno.setMatricula(Desenvolvimento de Software); //Preenchendo as propriedades do objeto

// curso.setDescricao(“Curso só pra programadores”);

sessao.save(aluno); //Transformando o objeto transiente em um objeto

//persistente no banco de dados

transaction.commit(); //Finalizando a transação

sessao.close(); //Fechando a sessão

}

}

até mais !!!fui!!! :frowning:

Criado 24 de setembro de 2010
Ultima resposta 7 de out. de 2010
Respostas 3
Participantes 2