Bom pessoal, estou tendo um problema com herança entre entidades JPA e apesar de ter procurado solução em vários lugares, não consegui chegar a nenhuma conclusão. O problema está descrito abaixo.
Em uma aplicação possuo o seguinte relacionamento:
--------------Pessoa
--------------- /----\
-----------Juridica--Fisica
-----------------------|
-----------------Funcionario
O relacionamento direto entre os 2 tipos de pessoa (fisica e juridica) e a Pessoa está funcionando ok, o problema é na inserção de funcionários. O funcionário só pode ser inserido para uma pessoa física já existente, logo pensei em pegar os dados dessa pessoa, instanciar um funcionario, setar os dados da pessoa ja existente (inclusive o id) mais os dados relativos ao funcionário e dar um update em Pessoa utilizando o método merge(). Porém quando faço isso o hibernate está criando um NOVO registro na tabela Pessoa e associando esse registro ao funcionário.
Gostaria de saber se existe alguma maneira de eu dizer ao hibernate pra usar um registro pai já existente e somente adicionar um novo filho relacionado a esse pai.
Abaixo vou postar minhas entidades para que fique mais claro o entendimento do problema.
import static javax.persistence.InheritanceType.JOINED;
import java.util.Date;
import java.util.List;
import javax.persistence.AttributeOverride;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Inheritance;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.TableGenerator;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import br.com.model.to.PersistentObject;
import br.com.model.to.iht.endereco.EnderecoTO;
import br.com.model.to.iht.telefone.TelefoneTO;
@Entity
@Table(name="IHT_PESSOA", schema = "teste")
@Inheritance(strategy = JOINED)
@AttributeOverride(name="id", column=@Column(name="IDPESSOA"))
@TableGenerator(name="TB_SEQ", table="BD_SEQUENCE",
pkColumnName="DESCRIPTION", valueColumnName="NEXTVAL", pkColumnValue="SEQ_PESSOA",
allocationSize=1)
public class PessoaTO extends PersistentObject {
private static final long serialVersionUID = -7297531372204608112L;
@Column(insertable=true, updatable = true, name = "NOME")
private String nome;
@Temporal(TemporalType.DATE)
@Column(name = "DTCADASTRO", insertable = true, updatable = true)
private Date dtCadastro;
@Column(name = "OBSERVACAO", insertable = true, updatable = true)
private String observacao;
@ManyToOne(cascade = CascadeType.PERSIST)
@JoinColumn(name = "IDENDERECO", insertable = true, updatable = true)
private EnderecoTO endereco;
@OneToMany(mappedBy="pessoa")
private List<TelefoneTO> telefones;
@Column(name = "ENABLE", insertable = true, updatable = true)
private Boolean enable;
//Métodos get e set dos atributos
}
import static javax.persistence.InheritanceType.JOINED;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Inheritance;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import br.com.model.to.app.domain.ConstantTO;
@Entity
@Table(name="IHT_PESSOA_FISICA", schema = "teste")
@Inheritance(strategy = JOINED)
public class PessoaFisicaTO extends PessoaTO {
private static final long serialVersionUID = -7964310253850937814L;
@Column(insertable=true, updatable = true, name = "CPF")
private String cpf;
@Column(insertable=true, updatable = true, name = "RG")
private String rg;
@ManyToOne
@JoinColumn(name = "IDSEXO", insertable = true, updatable = true)
private ConstantTO sexo;
@ManyToOne
@JoinColumn(name = "IDPROFISSAO", insertable = true, updatable = true)
private ConstantTO profissao;
@Column(insertable=true, updatable = true, name = "EMAIL")
private String email;
@Temporal(TemporalType.DATE)
@Column(insertable=true, updatable = true, name = "DTNASCIMENTO")
private Date dtNascimento;
//Métodos get e set dos atributos
}
import static javax.persistence.InheritanceType.JOINED;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Inheritance;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import br.com.model.to.app.domain.ConstantTO;
import br.com.model.to.app.user.UserTO;
@Entity
@Table(name="IHT_FUNCIONARIO", schema = "teste")
@Inheritance(strategy = JOINED)
public class FuncionarioTO extends PessoaFisicaTO{
private static final long serialVersionUID = 2246504166686321668L;
@ManyToOne
@JoinColumn(name = "IDCARGO", insertable = true, updatable = true)
private ConstantTO cargo;
@Column(name = "NUMFUNCIONARIO", insertable = true, updatable = true)
private String numFuncionario;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "DTADMISSAO", insertable = true, updatable = true)
private Date dtAdmissao;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "DTDEMISSAO", insertable = true, updatable = true)
private Date dtDemissao;
@ManyToOne
@JoinColumn(name = "IDUSER", insertable = true, updatable = true)
private UserTO user;
@Column(name = "OBSERVACAO", insertable = true, updatable = true)
private String observacaoFunc;
//Métodos get e set dos atributos
}