在我的项目中,我有以下超类及其继承者:
public class Pessoa
public class PessoaJuridica extends Pessoa
public class Cliente extends PessoaJuridica
我在Hibernate中使用Table per Subclass作为继承方法。
所有插入都正确完成,但是当我尝试查询Pessoa类时,将Id作为参数传递,它仅从继承树的最后一个类返回数据。
所以,当我打电话
Query qry = session.createQuery("from Pessoa where id = :id");
qry.setParameter("id", 17);
System.out.println( qry.list() );
它仅打印Cliente类的id = 17的注册表记录...
我做错了代码吗?以下是该项目的来源 数据库脚本
CREATE TABLE public.endereco
(
_id integer NOT NULL DEFAULT nextval('endereco__id_seq'::regclass),
tipo character(1),
endereco character varying(255),
numero bigint,
complemento character varying(100),
bairro character varying(100),
cidade character varying(100),
uf character varying(2),
cep character varying(9),
status character(1),
CONSTRAINT endereco_pkey PRIMARY KEY (_id),
CONSTRAINT endereco_status_check CHECK (status = ANY (ARRAY['A'::bpchar, 'I'::bpchar])),
CONSTRAINT endereco_tipo_check CHECK (tipo = ANY (ARRAY['P'::bpchar, 'C'::bpchar, 'E'::bpchar]))
)
CREATE TABLE public.pessoa
(
_id integer NOT NULL DEFAULT nextval('pessoa__id_seq'::regclass),
id_endereco integer,
nome character varying(255),
status character(1),
CONSTRAINT pessoa_pkey PRIMARY KEY (_id),
CONSTRAINT fk_pessoa_endereco FOREIGN KEY (id_endereco)
REFERENCES public.endereco (_id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT pessoa_status_check CHECK (status = ANY (ARRAY['A'::bpchar, 'I'::bpchar]))
)
CREATE TABLE public.pessoa_juridica
(
id_pessoa integer NOT NULL,
cnpj character varying(20),
insc_est character varying(20),
razao_social character varying(255),
CONSTRAINT pessoa_juridica_pkey PRIMARY KEY (id_pessoa),
CONSTRAINT fk_pj_pessoa FOREIGN KEY (id_pessoa)
REFERENCES public.pessoa (_id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)
CREATE TABLE public.cliente
(
id_pessoa integer NOT NULL,
telefone character varying(15),
CONSTRAINT fk_cli_pjuridica FOREIGN KEY (id_pessoa)
REFERENCES public.pessoa (_id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)
课程
public class Endereco {
private int _id;
private char tipo;
private String endereco;
private long numero;
private String complemento;
private String bairro;
private String cidade;
private String uf;
private String cep;
private char status;
private Set<Pessoa> pessoas;
//Getters + Setters
}
public class Pessoa {
private int _id;
private Endereco endereco;
private String nome;
private char status;
}
public class PessoaJuridica extends Pessoa {
private String razaoSocial;
private String cnpj;
private String inscricaoEstadual;
}
public class Cliente extends PessoaJuridica{
private String telefone;
}
测试类
public class TestaSelectCliente {
public static void main(String[] args){
SessionFactory sf = HibernateUtil.getSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
Query qry = session.createQuery("from Pessoa");
System.out.println(qry.list());
Query qry1 = session.createQuery("from Pessoa where id= :id");
qry1.setParameter("id", 17);
System.out.println(qry1.list());
}
}
qry输出: [Cliente(telefone =(46)3225 - 6234),PessoaJuridica(razaoSocial = FRANCISCO S / A,cnpj = 087.971.169-80,inscricaoEstadual = 4.863.828), PessoaFisica(cpf = 087.971.169-80,rg = 4.863.828,dataNascimento = 2016-01-27)]
所有表格中的所有记录&lt;qry1输出: [Cliente(telefone =(46)3225 - 6234)]
来自层次结构表上最后一个类的数据。&lt;
我做错了什么/忘了做什么?
答案 0 :(得分:0)
这是映射XML:
<joined-subclass
name="br.com.koinonia.igrejahabil.model.entities.pessoa.PessoaFisica"
extends="br.com.koinonia.igrejahabil.model.entities.pessoa.Pessoa"
table="PESSOA_FISICA"
>
<key column="id_pessoa" />
<property name="cpf" column="CPF" />
<property name="rg" column="RG" />
<property name="dataNascimento" type="date" column="DATA_NASCIMENTO" />
</joined-subclass>
<joined-subclass
name="br.com.koinonia.igrejahabil.model.entities.pessoa.PessoaJuridica"
extends="br.com.koinonia.igrejahabil.model.entities.pessoa.Pessoa"
table="PESSOA_JURIDICA"
>
<key column="id_pessoa" />
<property name="razaoSocial" column="RAZAO_SOCIAL" />
<property name="cnpj" column="CNPJ" />
<property name="inscricaoEstadual" column="INSC_EST" />
<joined-subclass
name="br.com.koinonia.igrejahabil.model.entities.pessoa.juridica.Cliente"
extends="br.com.koinonia.igrejahabil.model.entities.pessoa.PessoaJuridica"
table="CLIENTE"
>
<key column="id_pessoa" />
<property name="telefone" column="TELEFONE" />
</joined-subclass>
</joined-subclass>
</class>
<class name="br.com.koinonia.igrejahabil.model.entities.endereco.Endereco"
table="ENDERECO" >
<id name="_id" column="_ID">
<generator class="native" />
</id>
<property name="endereco" column="ENDERECO" />
<property name="numero" column="NUMERO" />
<property name="complemento" column="COMPLEMENTO" />
<property name="bairro" column="bairro" />
<property name="cidade" column="cidade" />
<property name="uf" column="uf" />
<property name="cep" column="cep" />
<property name="tipo" column="tipo" type="character" />
<property name="status" column="status" type="character" />
<set name="pessoas" table="PESSOA" inverse="true" lazy="true" fetch="select">
<key column="_ID" not-null="true" />
<one-to-many class="br.com.koinonia.igrejahabil.model.entities.pessoa.Pessoa"/>
</set>
</class>