I have the following classes:
Super Class Pessoa
Code:
@Entity
@Table(name = "pessoa", schema = "cadastro")
@Inheritance(strategy = InheritanceType.JOINED)
public class Pessoa implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(...GENERATE STRATEGY)
private Integer idpessoa;
//... FIELDS, GETERS AND SETTERS
Child Pessoa Fisica
Code:
@Entity
@Table(name = "pessoa_fisica", schema = "cadastro")
@PrimaryKeyJoinColumn(name = "pessoa")
public class PessoaFisica extends Pessoa {
//... FIELDS, GETERS AND SETTERS
Another Child Pessoa Juridica
Code:
@Entity
@Table(name = "pessoa_juridica", schema = "cadastro")
@PrimaryKeyJoinColumn(name = "pessoa")
public class PessoaJuridica extends Pessoa {
//... FIELDS, GETERS AND SETTERS
When i execute the following query...
Code:
SELECT new PessoaFisica(pf.idpessoa, pf.nome, pf.cpf) FROM PessoaFisica pf
I get the error:
column pessoafisi0_.idpessoa does not exist because Hibernate try generate the query using child table alias and the field name from the parent table on line 2
Generated Query:
Code:
select
pessoafisi0_.idpessoa as col_0_0_,
pessoafisi0_1_.nome as col_1_0_,
pessoafisi0_.cpf as col_0_1_,
from
cadastro.pessoa_fisica pessoafisi0_
inner join
cadastro.pessoa pessoafisi0_1_
on pessoafisi0_.pessoa=pessoafisi0_1_.idpessoa
The correct is get field pessoa from pessoafisi0_ (Child Table) or field idpessoa from pessoafisi0_1_ (Parent Table)
pessoafisi0_.idpessoa as col_0_0_
-> WRONGpessoafisi0_.pessoa as col_0_0_
-> CORRECTpessoafisi0_1_.idpessoa as col_0_0_
-> CORRECTIf i do the same with PessoaJuridica the query was generated with success as follows:
Query:
Code:
SELECT new PessoaJuridica(pj.idpessoa, pj.nome, pj.cnpj) FROM PessoaJuridica pj
Generated Query:
Code:
select
pessoajuri0_.pessoa as col_0_0_,
pessoajuri0_1_.nome as col_1_0_,
pessoajuri0_.cnpj as col_2_0_
from
cadastro.pessoa_juridica pessoajuri0_
inner join
cadastro.pessoa pessoajuri0_1_
on pessoajuri0_.pessoa=pessoajuri0_1_.idpessoa
Using Hibernate Core 4.0.1, Hibernate Entity Manager 4.0.1, Hibernate Commons Annotations 4.0.1 and Spring 3.1
Am i doing something wrong or this is a bug?
Sorry bad english :(
Thanks in advance!