Pessoal!
Estou com um problema ao utilizar JPA (toplink)/Jaybird com relacionamento um para muitos:
EmpresaFacadeRemote empresa = (EmpresaFacadeRemote) ic.lookup("br.teste.app.beans.EmpresaFacadeRemote");
empresa.createEmpresa(jtfRasaoSocial.getText(), jtfNomeFantasia.getText(), jtfCpfCnpj.getText(), jtfRgInscEstadual.getText());
empresa.addMeioContato(meiosContatoList);
SessionBeans:
package br.teste.app.beans;
import br.teste.app.entidades.MeioContato;
import br.teste.app.entidades.Pessoa;
import java.util.List;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
/**
*
* @author kurumin
*/
@Stateful
public class EmpresaFacade implements EmpresaFacadeRemote {
@PersistenceContext(type=PersistenceContextType.TRANSACTION)
private EntityManager em;
public Pessoa createEmpresa(String rasaoSocial, String nomeFantasia, String cnpj, String inscricaiEstadual) {
this.empresa = new Pessoa(rasaoSocial, nomeFantasia, cnpj, inscricaiEstadual,"EM",'J');
em.persist(empresa);
return empresa;
}
public Pessoa createEmpresa(Pessoa empresa, List<MeioContato> meiosContatoList) {
this.empresa = empresa;
empresa.setMeioContatoCollection(meiosContatoList);
em.persist(empresa);
return empresa;
}
public void addMeioContato(List<MeioContato> meiosContatoList) {
this.empresa.setMeioContatoCollection(meiosContatoList);
em.merge(empresa);
}
private Pessoa empresa;
}
Beans de entidade
package br.teste.app.entidades;
import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
/**
*
* @author kurumin
*/
@Entity
@Table(name = "PESSOA")
@NamedQueries({@NamedQuery(name = "Pessoa.findByCodigo", query = "SELECT p FROM Pessoa p WHERE p.codigo = :codigo"), @NamedQuery(name = "Pessoa.findByNome", query = "SELECT p FROM Pessoa p WHERE p.nome = :nome"), @NamedQuery(name = "Pessoa.findByApelido", query = "SELECT p FROM Pessoa p WHERE p.apelido = :apelido"), @NamedQuery(name = "Pessoa.findByCpfCnpj", query = "SELECT p FROM Pessoa p WHERE p.cpfCnpj = :cpfCnpj"), @NamedQuery(name = "Pessoa.findByRgInscricao", query = "SELECT p FROM Pessoa p WHERE p.rgInscricao = :rgInscricao"), @NamedQuery(name = "Pessoa.findByOrgaoExpedidor", query = "SELECT p FROM Pessoa p WHERE p.orgaoExpedidor = :orgaoExpedidor"), @NamedQuery(name = "Pessoa.findByUf", query = "SELECT p FROM Pessoa p WHERE p.uf = :uf"), @NamedQuery(name = "Pessoa.findByFotoLogomarca", query = "SELECT p FROM Pessoa p WHERE p.fotoLogomarca = :fotoLogomarca"), @NamedQuery(name = "Pessoa.findByTipo", query = "SELECT p FROM Pessoa p WHERE p.tipo = :tipo"), @NamedQuery(name = "Pessoa.findByTipoPessoa", query = "SELECT p FROM Pessoa p WHERE p.tipoPessoa = :tipoPessoa"), @NamedQuery(name = "Pessoa.findByDataCadastro", query = "SELECT p FROM Pessoa p WHERE p.dataCadastro = :dataCadastro"), @NamedQuery(name = "Pessoa.findByObservacao", query = "SELECT p FROM Pessoa p WHERE p.observacao = :observacao")})
public class Pessoa implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "CODIGO", nullable = false)
private Integer codigo;
@Column(name = "NOME", nullable = false)
private String nome;
@Column(name = "APELIDO")
private String apelido;
@Column(name = "CPF_CNPJ", nullable = false)
private String cpfCnpj;
@Column(name = "RG_INSCRICAO")
private String rgInscricao;
@Column(name = "ORGAO_EXPEDIDOR")
private String orgaoExpedidor;
@Column(name = "UF")
private String uf;
@Column(name = "FOTO_LOGOMARCA")
private String fotoLogomarca;
@Column(name = "TIPO", nullable = false)
private String tipo;
@Column(name = "TIPO_PESSOA", nullable = false)
private char tipoPessoa;
@Column(name = "DATA_CADASTRO")
@Temporal(TemporalType.DATE)
private Date dataCadastro;
@Column(name = "OBSERVACAO")
private String observacao;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "pessoaCodg")
private Collection<MeioContato> meioContatoCollection;
public Pessoa() {
}
public Pessoa(String nome, String apelido, String cpfCnpj, String rgInscricao,
String orgaoExpedidor, String uf, String tipo, char tipoPessoa) {
this.nome = nome;
this.apelido = apelido;
this.cpfCnpj = cpfCnpj;
this.rgInscricao = rgInscricao;
this.orgaoExpedidor = orgaoExpedidor;
this.uf = uf;
this.tipo = tipo;
this.tipoPessoa = tipoPessoa;
}
public Pessoa(String nome, String apelido, String cpfCnpj, String rgInscricao,
String tipo, char tipoPessoa) {
this.nome = nome;
this.apelido = apelido;
this.cpfCnpj = cpfCnpj;
this.rgInscricao = rgInscricao;
this.tipo = tipo;
this.tipoPessoa = tipoPessoa;
}
public Integer getCodigo() {
return codigo;
}
public void setCodigo(Integer codigo) {
this.codigo = codigo;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getApelido() {
return apelido;
}
public void setApelido(String apelido) {
this.apelido = apelido;
}
public String getCpfCnpj() {
return cpfCnpj;
}
public void setCpfCnpj(String cpfCnpj) {
this.cpfCnpj = cpfCnpj;
}
public String getRgInscricao() {
return rgInscricao;
}
public void setRgInscricao(String rgInscricao) {
this.rgInscricao = rgInscricao;
}
public String getOrgaoExpedidor() {
return orgaoExpedidor;
}
public void setOrgaoExpedidor(String orgaoExpedidor) {
this.orgaoExpedidor = orgaoExpedidor;
}
public String getUf() {
return uf;
}
public void setUf(String uf) {
this.uf = uf;
}
public String getFotoLogomarca() {
return fotoLogomarca;
}
public void setFotoLogomarca(String fotoLogomarca) {
this.fotoLogomarca = fotoLogomarca;
}
public String getTipo() {
return tipo;
}
public void setTipo(String tipo) {
this.tipo = tipo;
}
public char getTipoPessoa() {
return tipoPessoa;
}
public void setTipoPessoa(char tipoPessoa) {
this.tipoPessoa = tipoPessoa;
}
public Date getDataCadastro() {
return dataCadastro;
}
public void setDataCadastro(Date dataCadastro) {
this.dataCadastro = dataCadastro;
}
public String getObservacao() {
return observacao;
}
public void setObservacao(String observacao) {
this.observacao = observacao;
}
public Collection<MeioContato> getMeioContatoCollection() {
return meioContatoCollection;
}
public void setMeioContatoCollection(Collection<MeioContato> meioContatoCollection) {
this.meioContatoCollection = meioContatoCollection;
}
@Override
public int hashCode() {
int hash = 0;
hash += (codigo != null ? codigo.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Pessoa)) {
return false;
}
Pessoa other = (Pessoa) object;
if ((this.codigo == null && other.codigo != null) || (this.codigo != null && !this.codigo.equals(other.codigo))) {
return false;
}
return true;
}
@Override
public String toString() {
return "br.teste.app.entidades.Pessoa[codigo=" + codigo + "]";
}
}
package br.teste.app.entidades;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
/**
*
* @author kurumin
*/
@Entity
@Table(name = "MEIO_CONTATO")
@NamedQueries({@NamedQuery(name = "MeioContato.findByMeioContato", query = "SELECT m FROM MeioContato m WHERE m.meioContato = :meioContato"), @NamedQuery(name = "MeioContato.findByTipo", query = "SELECT m FROM MeioContato m WHERE m.tipo = :tipo"), @NamedQuery(name = "MeioContato.findByResponsavel", query = "SELECT m FROM MeioContato m WHERE m.responsavel = :responsavel"), @NamedQuery(name = "MeioContato.findByDescricao", query = "SELECT m FROM MeioContato m WHERE m.descricao = :descricao"), @NamedQuery(name = "MeioContato.findBySetor", query = "SELECT m FROM MeioContato m WHERE m.setor = :setor")})
public class MeioContato implements Serializable {
private static final long serialVersionUID = 1L;
@Column(name = "MEIO_CONTATO", nullable = false)
private String meioContato;
@Column(name = "TIPO", nullable = false)
private String tipo;
@Column(name = "RESPONSAVEL")
private String responsavel;
@Id
@Column(name = "DESCRICAO", nullable = false)
private String descricao;
@Column(name = "SETOR")
private String setor;
@JoinColumn(name = "PESSOA_CODG", referencedColumnName = "CODIGO")
@ManyToOne
private Pessoa pessoaCodg;
public MeioContato() {
}
public MeioContato(String tipo, String meioContato, String setor, String resposavel, String descricao) {
this.descricao = descricao;
this.meioContato = meioContato;
this.tipo = tipo;
this.responsavel = resposavel;
this.setor = setor;
}
public String getMeioContato() {
return meioContato;
}
public void setMeioContato(String meioContato) {
this.meioContato = meioContato;
}
public String getTipo() {
return tipo;
}
public void setTipo(String tipo) {
this.tipo = tipo;
}
public String getResponsavel() {
return responsavel;
}
public void setResponsavel(String responsavel) {
this.responsavel = responsavel;
}
public String getDescricao() {
return descricao;
}
public void setDescricao(String descricao) {
this.descricao = descricao;
}
public String getSetor() {
return setor;
}
public void setSetor(String setor) {
this.setor = setor;
}
public Pessoa getPessoaCodg() {
return pessoaCodg;
}
public void setPessoaCodg(Pessoa pessoaCodg) {
this.pessoaCodg = pessoaCodg;
}
@Override
public int hashCode() {
int hash = 0;
hash += (descricao != null ? descricao.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof MeioContato)) {
return false;
}
MeioContato other = (MeioContato) object;
if ((this.descricao == null && other.descricao != null) || (this.descricao != null && !this.descricao.equals(other.descricao))) {
return false;
}
return true;
}
@Override
public String toString() {
return "br.teste.app.entidades.MeioContato[descricao=" + descricao + "]";
}
}
[color=red]Exception [TOPLINK-4002] (Oracle TopLink Essentials - 2.0.1 (Build b04-fcs (04/11/2008))): oracle.toplink.essentials.exceptions.DatabaseException
Internal Exception: org.firebirdsql.jdbc.FBSQLException: GDS Exception. 335544665. violation of PRIMARY or UNIQUE KEY constraint "UNQ_PESSOA_CPF" on table "PESSOA"
Error Code: 335544665
Call: INSERT INTO PESSOA (CODIGO, UF, FOTO_LOGOMARCA, APELIDO, TIPO, RG_INSCRICAO, TIPO_PESSOA, NOME, DATA_CADASTRO, ORGAO_EXPEDIDOR, OBSERVACAO, CPF_CNPJ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
bind => [null, null, null, kjkjk, EM, jkjljlj, J, julkjlkjl, null, null, null, jkjkj222][/color]
Outra forma de chama aos métodos:
EmpresaFacadeRemote empresa = (EmpresaFacadeRemote) ic.lookup("br.teste.app.beans.EmpresaFacadeRemote");
private Pessoa entidadePessoa = new Pessoa(jtfRasaoSocial.getText(), jtfNomeFantasia.getText(), jtfCpfCnpj.getText(), jtfRgInscEstadual.getText(),"EM",'J');
empresa.createEmpresa(entidadePessoa,meiosContatoList);
[color=red]Local Exception Stack:
Exception [TOPLINK-4002] (Oracle TopLink Essentials - 2.0.1 (Build b04-fcs (04/11/2008))): oracle.toplink.essentials.exceptions.DatabaseException
Internal Exception: org.firebirdsql.jdbc.FBSQLException: GDS Exception. 335544347. validation error for column PESSOA_CODG, value "*** null ***"
Error Code: 335544347
Call: INSERT INTO MEIO_CONTATO (DESCRICAO, RESPONSAVEL, TIPO, SETOR, MEIO_CONTATO, PESSOA_CODG) VALUES (?, ?, ?, ?, ?, ?)
bind => [([telefone removido]), João da Silva, Residêncial, Compras, Residêncial, null]
Query: InsertObjectQuery(br.teste.app.entidades.MeioContato[descricao=([telefone removido])])
[/color]