Cascata Gera um erro não detectado ?!

9 respostas
juniorsatanas

Pessoal bom dia, estou a 2 horas neste erro e não sei identifica-lo, como programo só não tenho como pedir uma leitura de outro, vou usar vocês. R$ 1,00 para quem apontar o erro:

Segue as Classes e o erro:

package br.gov.nutec.modelo;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import org.hibernate.validator.Length;
import org.hibernate.validator.NotNull;
import javax.persistence.*;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;

@Entity
@Table(name="pessoa",schema="public")
public class Pessoa implements Serializable {

private static long serialVersionUID = 1L;
public static long getSerialVersionUID() {
return serialVersionUID;
    }
public static void setSerialVersionUID(long aSerialVersionUID) {
serialVersionUID = aSerialVersionUID;
    }   

        @Id
	//@GeneratedValue(strategy=GenerationType.IDENTITY)
        @GeneratedValue(strategy=GenerationType.SEQUENCE)
	@Column(name="pessoa_id")   // chave primaria
	private Long id;

        @NotNull
	@Length(min =5 , max = 40)
	@Column(name="nome")
	private String nome; 
        
        @Column(name="cpf", unique=true)
        private String cpf;
                
        @NotNull
        @Length(min =1 , max = 40)
        @Column(name="matricula")
	private String matricula;

        @Column(name="digina")
	private String digina;

        @Column(name="data_matricula")
        @Temporal(javax.persistence.TemporalType.DATE)
	private Date data_matricula;

        @Column(name="RG")
	private String RG;

        @Column(name="orgao_exp")
	private String orgao_exp;

        @Column(name="titulo_ele")
	private String titulo_ele;

        @Column(name="endereco")
	private String endereco;

        @Column(name="bairro")
	private String bairro;

        @Column(name="cidade")
	private String cidade;

        @Column(name="uf")
	private String uf;

        @Column(name="fone")
	private String fone;

        @Column(name="celular")
	private String celular;

	@Column(name="email")
	private String email;
	
	@Column(name="senha")
	private String senha;
        
        @Column(name="status")
	private String status;

        @Temporal(TemporalType.DATE)
	@Column(name="data_nasc")
	private Date data_nasc;

 /*************************************************/
 /*  relacionamento 1 Pessoa para n Mensalidade   */
 /*                                               */
 /*************************************************/
        @OneToMany(mappedBy="pessoa", fetch = FetchType.LAZY)
        @Cascade(CascadeType.ALL)
        private List<Mensalidade> Mensalidades;
 /*********************fim*****************************/
	public void setNome(String nome) {
		System.out.println("Pessoa >> nome : "  + this.nome);
		
		this.nome = nome;
	}

	
	@Override
	public boolean equals(Object obj){
		if( (obj instanceof Pessoa) && ( ((Pessoa)obj).getCpf().equals(this.getCpf())) ){
			return true; 
		}else{
			return false; 
		}
	}

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 23 * hash + (this.getId() != null ? this.getId().hashCode() : 0);
        hash = 23 * hash + (this.getNome() != null ? this.getNome().hashCode() : 0);
        hash = 23 * hash + (this.getCpf() != null ? this.getCpf().hashCode() : 0);
        hash = 23 * hash + (this.getMatricula() != null ? this.getMatricula().hashCode() : 0);
        hash = 23 * hash + (this.getDigina() != null ? this.getDigina().hashCode() : 0);
        hash = 23 * hash + (this.getData_matricula() != null ? this.getData_matricula().hashCode() : 0);
        hash = 23 * hash + (this.getRG() != null ? this.getRG().hashCode() : 0);
        hash = 23 * hash + (this.getOrgao_exp() != null ? this.getOrgao_exp().hashCode() : 0);
        hash = 23 * hash + (this.getTitulo_ele() != null ? this.getTitulo_ele().hashCode() : 0);
        hash = 23 * hash + (this.getEndereco() != null ? this.getEndereco().hashCode() : 0);
        hash = 23 * hash + (this.getBairro() != null ? this.getBairro().hashCode() : 0);
        hash = 23 * hash + (this.getCidade() != null ? this.getCidade().hashCode() : 0);
        hash = 23 * hash + (this.getUf() != null ? this.getUf().hashCode() : 0);
        hash = 23 * hash + (this.getFone() != null ? this.getFone().hashCode() : 0);
        hash = 23 * hash + (this.getCelular() != null ? this.getCelular().hashCode() : 0);
        hash = 23 * hash + (this.getEmail() != null ? this.getEmail().hashCode() : 0);
        hash = 23 * hash + (this.getSenha() != null ? this.getSenha().hashCode() : 0);
        hash = 23 * hash + (this.getData_nasc() != null ? this.getData_nasc().hashCode() : 0);
        return hash;
    }
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getNome() {
        return nome;
    }
    public String getCpf() {
        return cpf;
    }
    public void setCpf(String cpf) {
        this.cpf = cpf;
    }
    public String getMatricula() {
        return matricula;
    }
    public void setMatricula(String matricula) {
        this.matricula = matricula;
    }
    public String getDigina() {
        return digina;
    }
    public void setDigina(String digina) {
        this.digina = digina;
    }
    public Date getData_matricula() {
        return data_matricula;
    }
    public void setData_matricula(Date data_matricula) {
        this.data_matricula = data_matricula;
    }
    public String getRG() {
        return RG;
    }
    public void setRG(String RG) {
        this.RG = RG;
    }
    public String getOrgao_exp() {
        return orgao_exp;
    }
    public void setOrgao_exp(String orgao_exp) {
        this.orgao_exp = orgao_exp;
    }
    public String getTitulo_ele() {
        return titulo_ele;
    }
    public void setTitulo_ele(String titulo_ele) {
        this.titulo_ele = titulo_ele;
    }
    public String getEndereco() {
        return endereco;
    }
    public void setEndereco(String endereco) {
        this.endereco = endereco;
    }
    public String getBairro() {
        return bairro;
    }
    public void setBairro(String bairro) {
        this.bairro = bairro;
    }
    public String getCidade() {
        return cidade;
    }
    public void setCidade(String cidade) {
        this.cidade = cidade;
    }
    public String getUf() {
        return uf;
    }
    public void setUf(String uf) {
        this.uf = uf;
    }
    public String getFone() {
        return fone;
    }
    public void setFone(String fone) {
        this.fone = fone;
    }
    public String getCelular() {
        return celular;
    }
    public void setCelular(String celular) {
        this.celular = celular;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getSenha() {
        return senha;
    }
    public void setSenha(String senha) {
        this.senha = senha;
    }
    public String getStatus() {
        return status;
    }
    public void setStatus(String status) {
        this.status = status;
    }
    public Date getData_nasc() {
        return data_nasc;
    }
    public void setData_nasc(Date data_nasc) {
        this.data_nasc = data_nasc;
    }
    public List<Mensalidade> getMensalidades() {
        return Mensalidades;
    }
    public void setMensalidades(List<Mensalidade> Mensalidades) {
        this.Mensalidades = Mensalidades;
    }
        
}
package br.gov.nutec.modelo;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.validator.NotNull;

@Entity
@Table(name="mensalidade",schema="anotacoes")
public class Mensalidade implements Serializable {
private static long serialVersionUID = 1L;
public static long getSerialVersionUID() {
return serialVersionUID;
    }
    public static void setSerialVersionUID(long aSerialVersionUID) {
        serialVersionUID = aSerialVersionUID;
    }

	@Id
        @NotNull
	@GeneratedValue(strategy=GenerationType.SEQUENCE)
	@Column(name="men_id")   // chave primaria
	private Long id;


        @Column(name="data_pag")
        @Temporal(javax.persistence.TemporalType.DATE)
	private Date data_pag;

        @Column(name="valor")
        private Long valor;

/*************************************************/
/*  relacionamento n Mensalidade para 1 Pessoa   */
/*                                               */
/*************************************************/
  @ManyToOne(fetch = FetchType.EAGER)
  @JoinColumn(name="pessoa_id",
                  insertable=true, updatable=true)
  @Fetch(FetchMode.JOIN)
  @Cascade(CascadeType.SAVE_UPDATE)

  private Pessoa pessoa_id;
 /*********************fim*****************************/
    @Override
     public boolean equals(Object obj){
		if( (obj instanceof Mensalidade) && ( ((Mensalidade)obj).getValor().equals(this.getValor())) ){
			return true;
		}else{
			return false;
		}
	}

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 41 * hash + (this.getId() != null ? this.getId().hashCode() : 0);
        hash = 41 * hash + (this.getData_pag() != null ? this.getData_pag().hashCode() : 0);
        hash = 41 * hash + (this.getValor() != null ? this.getValor().hashCode() : 0);
        return hash;
    }
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public Date getData_pag() {
        return data_pag;
    }
    public void setData_pag(Date data_pag) {
        this.data_pag = data_pag;
    }
    public Long getValor() {
        return valor;
    }
    public void setValor(Long valor) {
        this.valor = valor;
    }


}

9 Respostas

Alexandre_Saudate

O nome do mappedby tem que ser “pessoa_id” (de acordo com o nome da propriedade na Mensalidade).

Quer o número da minha conta pra passar o R$ 1,00 ?? =P

[]´s

juniorsatanas

asaudate Pior que tu ta certo ou melhor Ainda Bem Agora só falta arrumar o MB !

Abraço Valeu !

jr

pmlm

Outra coisa que pode dar problemas: o equals/hashCode não estão implementados correctamente. Elementos iguais não vão ter sempre valores de hash iguais.

juniorsatanas

pmlm Boa Tarde O próprio NetBeans faz !

Abraço !

pmlm

Mas se fez assim, fez mal!

juniorsatanas

pmlm Pois é !

Tenho que fazer isso na Mão !

pmlm

Convém não esquecer a regra de que dois objectos iguais têm hashCode igual. Não uso o netbeans mas deve haver maneira de escolher os campos que entram na comparação do equals e na construção do hashCode. Devem ser os mesmos campos. No teu caso tens muito menos campos no equals do que no hashCode.

juniorsatanas

asaudate Depositei na sua conta R$ 1.00,00 ,mas deu outro erro Rararara.

juniorsatanas

Deste problema pulei para este :

http://www.guj.com.br/posts/list/199228.java

Que no final estão relacionados !!

Criado 24 de fevereiro de 2010
Ultima resposta 25 de fev. de 2010
Respostas 9
Participantes 3