Duvida em mapeamento de herança com hibernate annotations

1 resposta
sergio_java

Galera é o seguinte, não estou conseguindo fazer o seguinte mapeamento corretamente.

Tenho uma classe chamada Pessoa, essa classe possui um relacionamento com TipoPessoa. Tenho outra classe chamada PessoaFisica.
PessoaFisica extende de TipoPessoa.

Ocorre o Seguinte erro:

14:22:40 INFO [EntityBinder] Bind entity br.com.sgfcasamento.modelo.PessoaFisica on table PessoaFisica Exception in thread "main" java.lang.ClassCastException: org.hibernate.mapping.JoinedSubclass cannot be cast to org.hibernate.mapping.RootClass at org.hibernate.cfg.annotations.PropertyBinder.bind(PropertyBinder.java:209) at org.hibernate.cfg.annotations.PropertyBinder.makePropertyValueAndBind(PropertyBinder.java:200) at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:1988) at org.hibernate.cfg.AnnotationBinder.processIdPropertiesIfNotAlready(AnnotationBinder.java:769) at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:733) at org.hibernate.cfg.AnnotationConfiguration.processArtifactsOfType(AnnotationConfiguration.java:636) at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:359) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1377) at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:954) at br.com.softwaremadeinbrazil.sgfcasamento.hibernate.GeradorDeBanco.main(GeradorDeBanco.java:12)

Dei uma lida sobre as anotações: @Inheritance(strategy = InheritanceType.JOINED),@PrimaryKeyJoinColumn. Porem não
entendi muito bem. Se alguem poder me dar uma explicação ficarei muito grato.

1 Resposta

brother2402

Então, também achei difícil de encontrar sobre isso, mas fica como resolvi o Problema:

@Entity

@Table(name = HER_PERSON)

@Inheritance(strategy = InheritanceType.JOINED)

public class Person implements IEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "PERSON_ID", unique = true)
private Long personId;

}

@Entity

@Table(name = HER_STUDENTS)

@NamedQueries({ @NamedQuery(name = studentByStatus, query = SELECT s FROM Student s WHERE s.studentActive = :active) })

@PrimaryKeyJoinColumn(name = PERSON_ID)

public class Student extends Person {

}

A anotação @Inheritance(strategy = InheritanceType.JOINED) especifica que será a tabela “pai” e será relacionada com outras tabelas pela coluna identificada como @Id.
A anotação @PrimaryKeyJoinColumn(name = “PERSON_ID”) se refere a coluna da tabela HER_STUDENTS que referencia a tabela HER_PERSON, nesse caso PERSON_ID.

As tabela seriam:

HER_STUDENTS

STUDENT_ID
PERSON_ID

e

HER_PERSON

PERSON_ID

Espero que consiga ajudar, foi o que encontrei e no meu caso utilizando Hibernate funcionou muito bem.

Criado 25 de outubro de 2010
Ultima resposta 29 de abr. de 2012
Respostas 1
Participantes 2