Criteria

30/06/2009

0

Olá pessoal! Tenho duas entidades Cadastro e Estrangeiro (Relacionamento - 1:N respectivamente). Ao tentar realizar uma busca utilizando criteria a IDE (NetBenas 6.5) apresenta a seguinte mensagem de erro: (Exception in thread ´main´ org.hibernate.QueryException: could not resolve property: numeroPassaporte of: br.com.comex.entidades.Estrangeiro).

Segue abaixo a estrutara das classes e seus mapeamentos:
=====Cadastro
package br.com.comex.entidades;

import java.math.BigInteger;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name=´TAB_COMEX_CADASTRO´)
public class Cadastro implements java.io.Serializable {

@Id
@Column(name=´ID_REGISTRO´, nullable=false)
private BigInteger idRegistro;

@Column(name=´NOME´, nullable=false)
private String nome;

@OneToMany(mappedBy=´idRegistro´, fetch=FetchType.LAZY)
List<Estrangeiro> estrangeiros;

public Cadastro() {
}

public Cadastro(BigInteger idRegistro, String nome, List<Estrangeiro> estrangeiros) {
this.idRegistro = idRegistro;
this.nome = nome;
this.estrangeiros = estrangeiros;
}

public List<Estrangeiro> getEstrangeiros() {
return estrangeiros;


public void setEstrangeiros(List<Estrangeiro> estrangeiros) {
this.estrangeiros = estrangeiros;
}

public BigInteger getIdRegistro() {
return idRegistro;
}

public void setIdRegistro(BigInteger idRegistro) {
this.idRegistro = idRegistro;
}

public String getNome() {
return nome;
}

public void setNome(String nome) {
this.nome = nome;
}

@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Cadastro other = (Cadastro) obj;
if (this.idRegistro != other.idRegistro && (this.idRegistro == null || !this.idRegistro.equals(other.idRegistro))) {
return false;
}
return true;
}

@Override
public int hashCode() {
int hash = 7;
hash = 19 * hash (this.idRegistro != null ? this.idRegistro.hashCode() : 0);
return hash;
}

===Estrangeiro
package br.com.comex.entidades;

import java.math.BigInteger;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name=´TAB_COMEX_ESTRANGEIRO´)



@Id
@Column(name=´ID_REGISTRO´)
@ManyToOne
@JoinColumn(name=´ID_REGISTRO´)
private BigInteger idRegistro;

@Column(name=´N_PASSAPORTE´)
private String numeroPassaport;

@Column(name=´PAIS´)
private int pais;


public Estrangeiro() {
}


public Estrangeiro(BigInteger idRegistro, String numeroPassaport, int pais) {
this.numeroPassaport = numeroPassaport;
this.pais = pais;

}


public String getNumeroPassaport() {
return numeroPassaport;
}

public void setNumeroPassaport(String numeroPassaport) {
this.numeroPassaport = numeroPassaport;
}

public int getPais() {
return pais;
}

public void setPais(int pais) {
this.pais = pais;
}

@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Estrangeiro other = (Estrangeiro) obj;
if (this.idRegistro != other.idRegistro && (this.idRegistro == null || !this.idRegistro.equals(other.idRegistro))) {
return false;
}
return true;
}

@Override
public int hashCode() {
int hash = 7;
hash = 79 * hash (this.idRegistro != null ? this.idRegistro.hashCode() : 0);

}


===Método Main
Session s = HibernateUtil.getInstance().getSession();

BigInteger i = new BigInteger(´96033903´);

Criteria c = (Criteria) s.createCriteria(Estrangeiro.class);
c.setFetchMode(´Cadastro´, FetchMode.JOIN);

c.add(Restrictions.and(Restrictions.like(´numeroPassaporte´, ´¬123456¬´),
Restrictions.eq(´idRegistro´, i) ));

c.list();

List<Estrangeiro> cadastro = c.list();



//List<Cadastro> c = s.createQuery(´select c from Cadastro c where c.estrangeiros.idRegistro = :id´).setBigInteger(´id´, i).list();

System.out.println(´A pessoa juridica é: ´ cadastro.get(0).getNumeroPassaport() );

s.getTransaction().commit();

HibernateUtil.getInstance().closeFactory();
Anonymous

Anonymous

Responder

Assista grátis a nossa aula inaugural

Assitir aula

Saiba por que programar é uma questão de
sobrevivência e como aprender sem riscos

Assistir agora

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar