Bom dia pessoal!
Não estou conseguindo deletar em cascata aguem pode me ajudar?
Retorna um erro de integridade ele tenta excluir a entidade forte antes da fraca.
código:
package model;
// Generated 10/12/2010 17:37:30 by Hibernate Tools 3.2.1.GA
import java.util.ArrayList;
import java.util.List;
import java.util.Date;
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.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
/**
* Pessoa generated by hbm2java
*/
@Entity
@Table(name = "PESSOA", schema = "TESTE")
@SequenceGenerator(name="PESSOA_SEQ", sequenceName="PESSOA_SEQ", allocationSize=1)
public class Pessoa implements java.io.Serializable {
private int codigo;
private String nome;
private String sexo;
private Date datanascimento;
private List<Pessoatelefone> pessoatelefones = new ArrayList<Pessoatelefone>();
public Pessoa() {
}
public Pessoa(int codigo) {
this.codigo = codigo;
}
public Pessoa(int codigo, String nome, String sexo, Date datanascimento, List<Pessoatelefone> pessoatelefones) {
this.codigo = codigo;
this.nome = nome;
this.sexo = sexo;
this.datanascimento = datanascimento;
this.pessoatelefones = pessoatelefones;
}
@Id
@Column(name = "CODIGO")
@GeneratedValue(generator="PESSOA_SEQ", strategy=GenerationType.SEQUENCE)
public int getCodigo() {
return this.codigo;
}
public void setCodigo(int codigo) {
this.codigo = codigo;
}
@Column(name = "NOME", length = 50)
public String getNome() {
return this.nome;
}
public void setNome(String nome) {
this.nome = nome;
}
@Column(name = "SEXO", length = 1)
public String getSexo() {
return this.sexo;
}
public void setSexo(String sexo) {
this.sexo = sexo;
}
@Temporal(TemporalType.DATE)
@Column(name = "DATANASCIMENTO", length = 7)
public Date getDatanascimento() {
return this.datanascimento;
}
public void setDatanascimento(Date datanascimento) {
this.datanascimento = datanascimento;
}
@OneToMany(mappedBy = "pessoa", cascade=CascadeType.ALL, fetch=FetchType.LAZY,targetEntity=Pessoatelefone.class)
public List<Pessoatelefone> getPessoatelefones() {
return pessoatelefones;
}
public void setPessoatelefones(List<Pessoatelefone> pessoatelefones) {
this.pessoatelefones = pessoatelefones;
}
}
package model;
// Generated 10/12/2010 17:37:30 by Hibernate Tools 3.2.1.GA
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
/**
* Pessoatelefone generated by hbm2java
*/
@Entity
@Table(name = "PESSOATELEFONE", schema = "TESTE")
@SequenceGenerator(name="PESSOATELEFONE_SEQ", sequenceName="PESSOATELEFONE_SEQ", allocationSize=1)
public class Pessoatelefone implements java.io.Serializable {
private int codigopessoatelefone;
private Pessoa pessoa;
private String numerotelefone;
public Pessoatelefone() {
}
public Pessoatelefone(int codigopessoatelefone) {
this.codigopessoatelefone = codigopessoatelefone;
}
public Pessoatelefone(int codigopessoatelefone, Pessoa pessoa, String numerotelefone) {
this.codigopessoatelefone = codigopessoatelefone;
this.pessoa = pessoa;
this.numerotelefone = numerotelefone;
}
@Id
@Column(name = "CODIGOPESSOATELEFONE")
@GeneratedValue(generator="PESSOATELEFONE_SEQ", strategy=GenerationType.SEQUENCE)
public int getCodigopessoatelefone() {
return this.codigopessoatelefone;
}
public void setCodigopessoatelefone(int codigopessoatelefone) {
this.codigopessoatelefone = codigopessoatelefone;
}
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name = "CODIGOPESSOA", nullable=false)
public Pessoa getPessoa() {
return this.pessoa;
}
public void setPessoa(Pessoa pessoa) {
this.pessoa = pessoa;
}
@Column(name = "NUMEROTELEFONE", length = 20)
public String getNumerotelefone() {
return this.numerotelefone;
}
public void setNumerotelefone(String numerotelefone) {
this.numerotelefone = numerotelefone;
}
}
metodo que tenta excluir
EntityManagerFactory emf = null;
EntityManager em = null;
try {
emf = Persistence.createEntityManagerFactory("teste");
em = emf.createEntityManager();
em.getTransaction().begin();
em.remove(em.merge(pessoa));
em.flush();
em.getTransaction().commit();
} catch (Exception e) {
em.getTransaction().rollback();
} finally {
if (em != null) {
em.close();
}
if (emf != null) {
emf.close();
}
}