Problema com Hibernate

6 respostas
V

Boa tarde,

Estou iniciando com Hibernate e estou tendo problema para mapear um relacionamento @OneToMany.
Segue as classes abaixo:

//CLASSE Locatario-------------------------------------------------------
package persistencia;

import java.util.Collection;

import javax.persistence.*;

import java.io.Serializable;

import org.hibernate.annotations.Cascade;

import org.hibernate.annotations.CascadeType;
@Entity

public class Locatario  implements Serializable

{

@Id

@GeneratedValue(strategy = GenerationType.SEQUENCE, generator =genCodLocatario)

@SequenceGenerator(name =genCodLocatario, sequenceName =genCodLocatario, allocationSize=1)

private int codLocatario;

private String nome;

private String cpf_cnpj;

private String tipo;

@OneToMany(mappedBy =“locatario”, fetch = FetchType.LAZY)
@Cascade(CascadeType.ALL)

private Collection<TelefoneLocatario> telefones;

//Métodos getters e setters
}

//CLASSE TelefoneLocatario------------------------------------------------------
package persistencia;

import javax.persistence.*;

import java.io.Serializable;

import org.hibernate.annotations.Cascade;

import org.hibernate.annotations.CascadeType;

import org.hibernate.annotations.Fetch;

import org.hibernate.annotations.FetchMode;
@Embeddable

public class TelefoneLocatario implements Serializable

{

private String telefone;

private String tipo;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name="codLocatario", insertable=true, updatable=true)
@Fetch(FetchMode.JOIN)
@Cascade(CascadeType.SAVE_UPDATE)

private Locatario locatario;

//Métodos getters e setters
}

o arquivo hibernate.cfg.xml está assim:

<?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>

&lt;!-- properties --&gt;
  &lt;property name="connection.driver_class"&gt; org.firebirdsql.jdbc.FBDriver &lt;/property&gt;
  &lt;property name="connection.url"&gt; jdbc:firebirdsql:localhost/3050:/usr/SistemaAlugueis/alugueis.gdb &lt;/property&gt;
  &lt;property name="dialect"&gt; org.hibernate.dialect.FirebirdDialect &lt;/property&gt;
  &lt;property name="show_sql"&gt;true&lt;/property&gt;
  &lt;property name="connection.username"&gt;SYSDBA&lt;/property&gt;
  &lt;property name="connection.password"&gt;masterkey&lt;/property&gt;
  &lt;property name="connection.pool_size"&gt;10&lt;/property&gt;
  
  &lt;!-- mapping classes --&gt;
  
  &lt;mapping class="persistencia.Locatario"/&gt;
  &lt;mapping class="persistencia.TelefoneLocatario"/&gt;
  
&lt;/session-factory&gt;

</hibernate-configuration>

Está aparecendo a seguinte mensagem de erro:

Exception in thread AWT-EventQueue-0 org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: persistencia.Locatario.telefones[persistencia.TelefoneLocatario]

at org.hibernate.cfg.annotations.CollectionBinder.bindManyToManySecondPass(CollectionBinder.java:1033)

at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:576)

at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:517)

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

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

at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:316)

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

at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:915)

O que estou fazendo de errado?

6 Respostas

V

HELP PLEASE!!! :lol:

Marky.Vasconcelos

Tá dando algum erro ou o que?

V

Está aparecendo a seguinte mensagem de erro:

Exception in thread AWT-EventQueue-0 org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: persistencia.Locatario.telefones[persistencia.TelefoneLocatario]

at org.hibernate.cfg.annotations.CollectionBinder.bindManyToManySecondPass(CollectionBinder.java:1033)

at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:576)

at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:517)

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

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

at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:316)

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

at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:915)
V

Consegui resolver o problema da seguinte maneira:

A minha classe TelefoneLocatario não tinha uma chave primária, já que não é necessário, pois apenas uma chave estrangeira com referência a classe Locatario já deveria resolver.
Como estava dando erro eu criei uma chave primária para TelefoneLocatario e resolveu o problema.

Porém, gostaria de saber se é possível utilizando o Hibernate, mapear uma classe que não possua uma chave primária???

ddduran

todo entity tem que ter um id, o que você quer fazer é um embedded object (acho)

da uma lida nos dois link abaixo são execelentes

http://docs.jboss.org/ejb3/app-server/HibernateAnnotations/reference/en/html_single/index.html
http://www.agiledata.org/essays/mappingObjects.html

V

O problema é que quando estou criando uma classe @embeddable está aparecendo este erro:

Exception in thread AWT-EventQueue-0 org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: persistencia.Locatario.telefones[persistencia.TelefoneLocatario]

at org.hibernate.cfg.annotations.CollectionBinder.bindManyToManySecondPass(CollectionBinder.java:1033)

at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:576)

at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:517)

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

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

at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:316)

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

at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:915)

Não sei mais o q faço, não é possível que não tem como mapear uma classe que não tenha uma chave primária!!! :x

Criado 13 de novembro de 2007
Ultima resposta 14 de nov. de 2007
Respostas 6
Participantes 3