Boa tarde.
Pessoal é o seguinte, tenho as classes Cliente, Pessoa e PessoaFisica onde PessoaFisica é filha de Pessoa. Abaixo as classes
package br.unipar.rectfer.entity;
import java.io.Serializable;
import java.util.Calendar;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
public class Pessoa implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer idPessoa;
@Temporal(TemporalType.TIMESTAMP)
private Calendar dataCadastro;
@Column
private String email;
@Column
private String telFixo;
@Column
private String celular;
@OneToOne(cascade = CascadeType.MERGE, mappedBy = "pessoa", fetch=FetchType.LAZY)
private Cliente cliente;
@OneToOne(cascade = CascadeType.MERGE, mappedBy = "pessoa", fetch=FetchType.LAZY)
private Fornecedor fornecedor;
@ManyToOne(cascade=CascadeType.MERGE, fetch=FetchType.LAZY, optional=false)
@JoinColumn(name="endereco", referencedColumnName="idEndereco")
private Endereco endereco;
public Calendar getDataCadastro() {
return dataCadastro;
}
public void setDataCadastro(Calendar dataCadastro) {
this.dataCadastro = dataCadastro;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getTelFixo() {
return telFixo;
}
public void setTelFixo(String telFixo) {
this.telFixo = telFixo;
}
public String getCelular() {
return celular;
}
public void setCelular(String celular) {
this.celular = celular;
}
public Cliente getCliente() {
return cliente;
}
public void setCliente(Cliente cliente) {
this.cliente = cliente;
//cliente.setPessoa(this);
}
public Fornecedor getFornecedor() {
return fornecedor;
}
public void setFornecedor(Fornecedor fornecedor) {
this.fornecedor = fornecedor;
}
public Integer getIdPessoa() {
return idPessoa;
}
public void setIdPessoa(Integer idPessoa) {
this.idPessoa = idPessoa;
}
public Endereco getEndereco() {
return endereco;
}
public void setEndereco(Endereco endereco) {
this.endereco = endereco;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((idPessoa == null) ? 0 : idPessoa.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Pessoa other = (Pessoa) obj;
if (idPessoa == null) {
if (other.idPessoa != null)
return false;
} else if (!idPessoa.equals(other.idPessoa))
return false;
return true;
}
}
package br.unipar.rectfer.entity;
import java.util.Calendar;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
public class PessoaFisica extends Pessoa{
/**
*
*/
private static final long serialVersionUID = 1L;
@Column(nullable=false, length=20)
private String cedula;
@Column(nullable=false)
private String nome;
@Temporal(TemporalType.TIMESTAMP)
private Calendar dataNascimento;
@Column(length=11, nullable=false)
private String ruc;
public String getCedula() {
return cedula;
}
public void setCedula(String cedula) {
this.cedula = cedula;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public Calendar getDataNascimento() {
return dataNascimento;
}
public void setDataNascimento(Calendar dataNascimento) {
this.dataNascimento = dataNascimento;
}
public String getRuc() {
return ruc;
}
public void setRuc(String ruc) {
this.ruc = ruc;
}
}
package br.unipar.rectfer.entity;
import java.io.Serializable;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
@Entity
public class Cliente implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@JoinColumn(name = "idPessoa", referencedColumnName = "idPessoa")
@OneToOne(cascade=CascadeType.MERGE, fetch=FetchType.LAZY)
private Pessoa pessoa;
public Cliente (){}
public Cliente(Pessoa pessoa){
this.pessoa = pessoa;
pessoa.setCliente(this);
}
public Pessoa getPessoa() {
return pessoa;
}
public void setPessoa(Pessoa pessoa){
this.pessoa = pessoa;
pessoa.setCliente(this);
}
}
A classe cliente recebe o id de pessoa para que na hora do select saber se esta pessoa é um cliente.
Quando mando gerar o banco, o hibernate gera tudo perfeitamente no banco. Mas quando faço um select ele retorna um NullPointerException.
Abaixo meu Dao
package br.unipar.rectfer.dao;
import java.util.List;
import javax.persistence.Query;
import br.unipar.rectfer.entity.PessoaFisica;
import br.unipar.rectfer.exception.DaoException;
public class PessoaFisicaDao extends DaoGenerico<PessoaFisica, Integer>{
@SuppressWarnings("unchecked")
public List<PessoaFisica> listaClientesFisicos() throws DaoException{
List<PessoaFisica> clientesFisicos = null;
String hql = "select object (p) from PessoaFisica as p ";
try{
Query query = getEntityManager().createQuery(hql);
clientesFisicos = query.getResultList();
}catch(DaoException e){
throw new DaoException("Erro ao listar os clientes fisicos", e);
}
return clientesFisicos;
}
}
E o erro que ele gera ao chamar esse método acima
Exception in thread "main" java.lang.NullPointerException
at org.hibernate.persister.entity.AbstractEntityPersister.loadByUniqueKey(AbstractEntityPersister.java:2314)
at org.hibernate.type.EntityType.loadByUniqueKey(EntityType.java:664)
at org.hibernate.type.EntityType.resolve(EntityType.java:444)
at org.hibernate.engine.internal.TwoPhaseLoad.doInitializeEntity(TwoPhaseLoad.java:168)
at org.hibernate.engine.internal.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:134)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:999)
at org.hibernate.loader.Loader.doQuery(Loader.java:878)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:293)
at org.hibernate.loader.Loader.doList(Loader.java:2382)
at org.hibernate.loader.Loader.doList(Loader.java:2368)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2198)
at org.hibernate.loader.Loader.list(Loader.java:2193)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:470)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:355)
at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:195)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1244)
at org.hibernate.internal.QueryImpl.list(QueryImpl.java:101)
at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:257)
at br.unipar.rectfer.dao.PessoaFisicaDao.listaClientesFisicos(PessoaFisicaDao.java:27)
at teste.teste.main(teste.java:59)
Com outras classes o select funciona, somente nestas classes que não esta funcionando.
Alguém já passou por isso. E algum bug do hibernate.
O hibernate que uso é o 4.1.6
Agradeço a todos que colaborarem
Abraços