ManyToMany x Criteria

6 respostas
G

Boa tarde a todos.

Estou engatinhando no Hibernate e estou com uma dúvida. Tenho uma tabela no BD chamada Laboratorio, outra chamada Exame. Estas tabelas tem um relacionamento N:N, onde foi criada uma outra tabela chamada laboratorio_exame, pois nesta tabela eu salvo o valor de cada exame de cada laboratório.
Pra começar, gostaria de saber se meu mapeamento manytomany está correto:

Classe Laboratorio

@Entity
@Table(name = "laboratorio")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "LaboratorioVO.findAll", query = "SELECT l FROM LaboratorioVO l"),
    @NamedQuery(name = "LaboratorioVO.findById", query = "SELECT l FROM LaboratorioVO l WHERE l.id = :id"),
    @NamedQuery(name = "LaboratorioVO.findByNome", query = "SELECT l FROM LaboratorioVO l WHERE l.nome = :nome"),
    @NamedQuery(name = "LaboratorioVO.findByEndereco", query = "SELECT l FROM LaboratorioVO l WHERE l.endereco = :endereco"),
    @NamedQuery(name = "LaboratorioVO.findByBairro", query = "SELECT l FROM LaboratorioVO l WHERE l.bairro = :bairro"),
    @NamedQuery(name = "LaboratorioVO.findByCidade", query = "SELECT l FROM LaboratorioVO l WHERE l.cidade = :cidade"),
    @NamedQuery(name = "LaboratorioVO.findByEstado", query = "SELECT l FROM LaboratorioVO l WHERE l.estado = :estado"),
    @NamedQuery(name = "LaboratorioVO.findByCep", query = "SELECT l FROM LaboratorioVO l WHERE l.cep = :cep"),
    @NamedQuery(name = "LaboratorioVO.findByCnpj", query = "SELECT l FROM LaboratorioVO l WHERE l.cnpj = :cnpj"),
    @NamedQuery(name = "LaboratorioVO.findByInscEstadual", query = "SELECT l FROM LaboratorioVO l WHERE l.inscEstadual = :inscEstadual"),
    @NamedQuery(name = "LaboratorioVO.findByTelefone", query = "SELECT l FROM LaboratorioVO l WHERE l.telefone = :telefone"),
    @NamedQuery(name = "LaboratorioVO.findByEmail", query = "SELECT l FROM LaboratorioVO l WHERE l.email = :email"),
    @NamedQuery(name = "LaboratorioVO.findByDiasAtendimento", query = "SELECT l FROM LaboratorioVO l WHERE l.diasAtendimento = :diasAtendimento")})
public class LaboratorioVO extends ValueObjectImpl implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "ID", nullable = false)
    private Integer id;
    @Column(name = "NOME", length = 100)
    private String nome;
    @Column(name = "ENDERECO", length = 100)
    private String endereco;
    @Column(name = "BAIRRO", length = 50)
    private String bairro;
    @Column(name = "CIDADE", length = 50)
    private String cidade;
    @Column(name = "ESTADO", length = 2)
    private String estado;
    @Column(name = "CEP", length = 8)
    private String cep;
    @Column(name = "CNPJ")
    private Integer cnpj;
    @Column(name = "INSC_ESTADUAL", length = 20)
    private String inscEstadual;
    @Column(name = "TELEFONE", length = 100)
    private String telefone;
    @Column(name = "EMAIL", length = 100)
    private String email;
    @Column(name = "DIAS_ATENDIMENTO", length = 100)
    private String diasAtendimento;

    public LaboratorioVO() {
    }

    public LaboratorioVO(Integer id) {
        this.id = id;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getNome() {
        return nome;
    }

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

    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 getEstado() {
        return estado;
    }

    public void setEstado(String estado) {
        this.estado = estado;
    }

    public String getCep() {
        return cep;
    }

    public void setCep(String cep) {
        this.cep = cep;
    }

    public Integer getCnpj() {
        return cnpj;
    }

    public void setCnpj(Integer cnpj) {
        this.cnpj = cnpj;
    }

    public String getInscEstadual() {
        return inscEstadual;
    }

    public void setInscEstadual(String inscEstadual) {
        this.inscEstadual = inscEstadual;
    }

    public String getTelefone() {
        return telefone;
    }

    public void setTelefone(String telefone) {
        this.telefone = telefone;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getDiasAtendimento() {
        return diasAtendimento;
    }

    public void setDiasAtendimento(String diasAtendimento) {
        this.diasAtendimento = diasAtendimento;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.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 LaboratorioVO)) {
            return false;
        }
        LaboratorioVO other = (LaboratorioVO) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.sismed.cadastros.java.LaboratorioVO[ id=" + id + " ]";
    }
    
}

Classe Exame

@Entity
@Table(name = "exame")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "ExameVO.findAll", query = "SELECT e FROM ExameVO e"),
    @NamedQuery(name = "ExameVO.findById", query = "SELECT e FROM ExameVO e WHERE e.id = :id"),
    @NamedQuery(name = "ExameVO.findByCodigo", query = "SELECT e FROM ExameVO e WHERE e.codigo = :codigo"),
    @NamedQuery(name = "ExameVO.findByNome", query = "SELECT e FROM ExameVO e WHERE e.nome = :nome")})
public class ExameVO extends ValueObjectImpl implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "ID", nullable = false)
    private Integer id;
    @Column(name = "CODIGO", nullable = false)
    private Integer codigo;
    @Column(name = "NOME", length = 60)
    private String nome;

    public ExameVO() {
    }

    public ExameVO(Integer id) {
        this.id = id;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }
    
    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;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.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 ExameVO)) {
            return false;
        }
        ExameVO other = (ExameVO) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.sismed.cadastros.java.Exame[ id=" + id + " ]";
    }
}

Classe LaboratorioExame

@Entity
@Table(name = "laboratorio_exame")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "LaboratorioExameVO.findAll", query = "SELECT l FROM LaboratorioExameVO l"),
    @NamedQuery(name = "LaboratorioExameVO.findByLaboratorioId", query = "SELECT l FROM LaboratorioExameVO l WHERE l.laboratorioExameVOPK.laboratorioId = :laboratorioId"),
    @NamedQuery(name = "LaboratorioExameVO.findByExameId", query = "SELECT l FROM LaboratorioExameVO l WHERE l.laboratorioExameVOPK.exameId = :exameId"),
    @NamedQuery(name = "LaboratorioExameVO.findByValor", query = "SELECT l FROM LaboratorioExameVO l WHERE l.valor = :valor")})
public class LaboratorioExameVO extends ValueObjectImpl implements Serializable {
    private static final long serialVersionUID = 1L;
    @EmbeddedId
    protected LaboratorioExameVOPK laboratorioExameVOPK;
    // @Max(value=?)  @Min(value=?)//if you know range of your decimal fields consider using these annotations to enforce field validation
    @Column(name = "VALOR", precision = 10, scale = 2)
    private Double valor;

    public LaboratorioExameVO() {
    }

    public LaboratorioExameVO(LaboratorioExameVOPK laboratorioExameVOPK) {
        this.laboratorioExameVOPK = laboratorioExameVOPK;
    }

    public LaboratorioExameVO(int laboratorioId, int exameId) {
        this.laboratorioExameVOPK = new LaboratorioExameVOPK(laboratorioId, exameId);
    }

    public LaboratorioExameVOPK getLaboratorioExameVOPK() {
        return laboratorioExameVOPK;
    }

    public void setLaboratorioExameVOPK(LaboratorioExameVOPK laboratorioExameVOPK) {
        this.laboratorioExameVOPK = laboratorioExameVOPK;
    }

    public Double getValor() {
        return valor;
    }

    public void setValor(Double valor) {
        this.valor = valor;
    }
    
    
    @Transient 
    public LaboratorioVO getLaboratorio() {  
        return this.getLaboratorio();  
    }  
    
    @Transient 
    public ExameVO getExame() {  
        return this.getExame();  
    }  

    

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (laboratorioExameVOPK != null ? laboratorioExameVOPK.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 LaboratorioExameVO)) {
            return false;
        }
        LaboratorioExameVO other = (LaboratorioExameVO) object;
        if ((this.laboratorioExameVOPK == null && other.laboratorioExameVOPK != null) || (this.laboratorioExameVOPK != null && !this.laboratorioExameVOPK.equals(other.laboratorioExameVOPK))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.sismed.cadastros.java.LaboratorioExameVO[ laboratorioExameVOPK=" + laboratorioExameVOPK + " ]";
    }
}

Classe LaboratorioExameVOPK

@Embeddable
public class LaboratorioExameVOPK implements Serializable {
    @Basic(optional = false)
    @Column(name = "LABORATORIO_ID", nullable = false)
    private int laboratorioId;
    @Basic(optional = false)
    @Column(name = "EXAME_ID", nullable = false)
    private int exameId;

    public LaboratorioExameVOPK() {
    }

    public LaboratorioExameVOPK(int laboratorioId, int exameId) {
        this.laboratorioId = laboratorioId;
        this.exameId = exameId;
    }

    public int getLaboratorioId() {
        return laboratorioId;
    }

    public void setLaboratorioId(int laboratorioId) {
        this.laboratorioId = laboratorioId;
    }

    public int getExameId() {
        return exameId;
    }

    public void setExameId(int exameId) {
        this.exameId = exameId;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (int) laboratorioId;
        hash += (int) exameId;
        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 LaboratorioExameVOPK)) {
            return false;
        }
        LaboratorioExameVOPK other = (LaboratorioExameVOPK) object;
        if (this.laboratorioId != other.laboratorioId) {
            return false;
        }
        if (this.exameId != other.exameId) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.sismed.cadastros.java.LaboratorioExameVOPK[ laboratorioId=" + laboratorioId + ", exameId=" + exameId + " ]";
    }
}

Este mapeamento está correto? E como eu faço para utilizar Criteria para trazer todos os dados da tabela laboratorio_exame, sem filtro algum, por enquanto?

Desde já agradeço a todos.

6 Respostas

mauricionarcizo

Relacionamento ManytoMany?? ficou estranho cara, voce quando vai fazer um exame e coletam seu sangue, esse seu sangue vai para vario laboratorios para analise???
eu acho mais correto um laboratorio para varios exames, varios exames para um laboratorio…
acho que é isso, desculpa se eu falei besteira, apenas estou tentando ajudar…

mauricionarcizo

E onde está o seu relacionamento das classes laboratorio e exame??? esse tipo de mapeamento é com hibernate annotations não???

G

mauricionarcizo, obrigado pelo retorno.

Na verdade, eu indico pessoas para laboratórios e eu tenho o cadastro de vários laboratórios que fazem o mesmo tipo de exame, por exemplo:
Laboratório A
Exame: Hemograma Completo
Valor: R$ 8,00

Laboratório B
Exame: Hemograma Completo
Valor: R$ 10,00

Com relação ao mapeamento, eu criei as tabelas no banco e pedi para o NetBeans criar os VOs anotados. Comparei com alguns exemplos na internet, e “parece” que está certo. Como estou iniciando no uso do hibernate, eu ainda não fiz mapeamento manytomany, portanto, não sei se está certo. Como seria essa estrutura?

mauricionarcizo

Atah, agora eu entendi, então dessa forma esta correta o teu relacionamento ManyToMany, tipo para usar esse relacionamento eu faço o seguinte aqui na minha empresa

//na classe laboratorio eu faria assim depois de voce declarar todos os atribos e mapea-los.
Exame exame = new Exame ()

@ManyToMany
public Exame getExame(){
return exame;
}

Eu acho que seria assim, espero ter ajudado, qualquer coisa espera um pouco que a galera mais experiente ja entra e te ajuda melhor!!

G

Obrigado mauricionarcizo. Vou fazer um teste e posto o resultado. E com relação a consulta? Como eu faria pra fazer todos os dados desta tabela? Pode ser utilizando Criteria ou HQL! Eu só consegui fazer um select com uma tabela simples!

G

Boa tarde meu povo!

Gerei as classes pelo Netbeans, criando uma nova entidade de banco de dados e o resultado foi o seguinte:

import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author glauber.ribeiro
 */
@Entity
@Table(name = "exame")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "ExameVO.findAll", query = "SELECT e FROM ExameVO e"),
    @NamedQuery(name = "ExameVO.findById", query = "SELECT e FROM ExameVO e WHERE e.id = :id"),
    @NamedQuery(name = "ExameVO.findByCodigo", query = "SELECT e FROM ExameVO e WHERE e.codigo = :codigo"),
    @NamedQuery(name = "ExameVO.findByNome", query = "SELECT e FROM ExameVO e WHERE e.nome = :nome")})
public class ExameVO implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "ID", nullable = false)
    private Integer id;
    @Basic(optional = false)
    @Column(name = "CODIGO", nullable = false)
    private int codigo;
    @Column(name = "NOME", length = 60)
    private String nome;

    public ExameVO() {
    }

    public ExameVO(Integer id) {
        this.id = id;
    }

    public ExameVO(Integer id, int codigo) {
        this.id = id;
        this.codigo = codigo;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public int getCodigo() {
        return codigo;
    }

    public void setCodigo(int codigo) {
        this.codigo = codigo;
    }

    public String getNome() {
        return nome;
    }

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

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.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 ExameVO)) {
            return false;
        }
        ExameVO other = (ExameVO) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.sismed.cadastros.java.ExameVO[ id=" + id + " ]";
    }
    
}
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author glauber.ribeiro
 */
@Entity
@Table(name = "laboratorio")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "LaboratorioVO.findAll", query = "SELECT l FROM LaboratorioVO l"),
    @NamedQuery(name = "LaboratorioVO.findById", query = "SELECT l FROM LaboratorioVO l WHERE l.id = :id"),
    @NamedQuery(name = "LaboratorioVO.findByNome", query = "SELECT l FROM LaboratorioVO l WHERE l.nome = :nome"),
    @NamedQuery(name = "LaboratorioVO.findByEndereco", query = "SELECT l FROM LaboratorioVO l WHERE l.endereco = :endereco"),
    @NamedQuery(name = "LaboratorioVO.findByBairro", query = "SELECT l FROM LaboratorioVO l WHERE l.bairro = :bairro"),
    @NamedQuery(name = "LaboratorioVO.findByCidade", query = "SELECT l FROM LaboratorioVO l WHERE l.cidade = :cidade"),
    @NamedQuery(name = "LaboratorioVO.findByEstado", query = "SELECT l FROM LaboratorioVO l WHERE l.estado = :estado"),
    @NamedQuery(name = "LaboratorioVO.findByCep", query = "SELECT l FROM LaboratorioVO l WHERE l.cep = :cep"),
    @NamedQuery(name = "LaboratorioVO.findByCnpj", query = "SELECT l FROM LaboratorioVO l WHERE l.cnpj = :cnpj"),
    @NamedQuery(name = "LaboratorioVO.findByInscEstadual", query = "SELECT l FROM LaboratorioVO l WHERE l.inscEstadual = :inscEstadual"),
    @NamedQuery(name = "LaboratorioVO.findByTelefone", query = "SELECT l FROM LaboratorioVO l WHERE l.telefone = :telefone"),
    @NamedQuery(name = "LaboratorioVO.findByEmail", query = "SELECT l FROM LaboratorioVO l WHERE l.email = :email"),
    @NamedQuery(name = "LaboratorioVO.findByDiasAtendimento", query = "SELECT l FROM LaboratorioVO l WHERE l.diasAtendimento = :diasAtendimento")})
public class LaboratorioVO implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "ID", nullable = false)
    private Integer id;
    @Column(name = "NOME", length = 100)
    private String nome;
    @Column(name = "ENDERECO", length = 100)
    private String endereco;
    @Column(name = "BAIRRO", length = 50)
    private String bairro;
    @Column(name = "CIDADE", length = 50)
    private String cidade;
    @Column(name = "ESTADO", length = 2)
    private String estado;
    @Column(name = "CEP", length = 10)
    private String cep;
    @Column(name = "CNPJ")
    private Integer cnpj;
    @Column(name = "INSC_ESTADUAL", length = 20)
    private String inscEstadual;
    @Column(name = "TELEFONE", length = 100)
    private String telefone;
    @Column(name = "EMAIL", length = 100)
    private String email;
    @Column(name = "DIAS_ATENDIMENTO", length = 500)
    private String diasAtendimento;

    public LaboratorioVO() {
    }

    public LaboratorioVO(Integer id) {
        this.id = id;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getNome() {
        return nome;
    }

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

    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 getEstado() {
        return estado;
    }

    public void setEstado(String estado) {
        this.estado = estado;
    }

    public String getCep() {
        return cep;
    }

    public void setCep(String cep) {
        this.cep = cep;
    }

    public Integer getCnpj() {
        return cnpj;
    }

    public void setCnpj(Integer cnpj) {
        this.cnpj = cnpj;
    }

    public String getInscEstadual() {
        return inscEstadual;
    }

    public void setInscEstadual(String inscEstadual) {
        this.inscEstadual = inscEstadual;
    }

    public String getTelefone() {
        return telefone;
    }

    public void setTelefone(String telefone) {
        this.telefone = telefone;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getDiasAtendimento() {
        return diasAtendimento;
    }

    public void setDiasAtendimento(String diasAtendimento) {
        this.diasAtendimento = diasAtendimento;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.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 LaboratorioVO)) {
            return false;
        }
        LaboratorioVO other = (LaboratorioVO) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.sismed.cadastros.java.LaboratorioVO[ id=" + id + " ]";
    }
    
}
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author glauber.ribeiro
 */
@Entity
@Table(name = "laboratorio_exame")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "LaboratorioExameVO.findAll", query = "SELECT l FROM LaboratorioExameVO l"),
    @NamedQuery(name = "LaboratorioExameVO.findByLaboratorioId", query = "SELECT l FROM LaboratorioExameVO l WHERE l.laboratorioExameVOPK.laboratorioId = :laboratorioId"),
    @NamedQuery(name = "LaboratorioExameVO.findByExameId", query = "SELECT l FROM LaboratorioExameVO l WHERE l.laboratorioExameVOPK.exameId = :exameId"),
    @NamedQuery(name = "LaboratorioExameVO.findByValor", query = "SELECT l FROM LaboratorioExameVO l WHERE l.valor = :valor")})
public class LaboratorioExameVO implements Serializable {
    private static final long serialVersionUID = 1L;
    @EmbeddedId
    protected LaboratorioExameVOPK laboratorioExameVOPK;
    // @Max(value=?)  @Min(value=?)//if you know range of your decimal fields consider using these annotations to enforce field validation
    @Column(name = "VALOR", precision = 10, scale = 2)
    private Double valor;

    public LaboratorioExameVO() {
    }

    public LaboratorioExameVO(LaboratorioExameVOPK laboratorioExameVOPK) {
        this.laboratorioExameVOPK = laboratorioExameVOPK;
    }

    public LaboratorioExameVO(int laboratorioId, int exameId) {
        this.laboratorioExameVOPK = new LaboratorioExameVOPK(laboratorioId, exameId);
    }

    public LaboratorioExameVOPK getLaboratorioExameVOPK() {
        return laboratorioExameVOPK;
    }

    public void setLaboratorioExameVOPK(LaboratorioExameVOPK laboratorioExameVOPK) {
        this.laboratorioExameVOPK = laboratorioExameVOPK;
    }

    public Double getValor() {
        return valor;
    }

    public void setValor(Double valor) {
        this.valor = valor;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (laboratorioExameVOPK != null ? laboratorioExameVOPK.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 LaboratorioExameVO)) {
            return false;
        }
        LaboratorioExameVO other = (LaboratorioExameVO) object;
        if ((this.laboratorioExameVOPK == null && other.laboratorioExameVOPK != null) || (this.laboratorioExameVOPK != null && !this.laboratorioExameVOPK.equals(other.laboratorioExameVOPK))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.sismed.cadastros.java.LaboratorioExameVO[ laboratorioExameVOPK=" + laboratorioExameVOPK + " ]";
    }
    
}
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Embeddable;

/**
 *
 * @author glauber.ribeiro
 */
@Embeddable
public class LaboratorioExameVOPK implements Serializable {
    @Basic(optional = false)
    @Column(name = "LABORATORIO_ID", nullable = false)
    private int laboratorioId;
    @Basic(optional = false)
    @Column(name = "EXAME_ID", nullable = false)
    private int exameId;

    public LaboratorioExameVOPK() {
    }

    public LaboratorioExameVOPK(int laboratorioId, int exameId) {
        this.laboratorioId = laboratorioId;
        this.exameId = exameId;
    }

    public int getLaboratorioId() {
        return laboratorioId;
    }

    public void setLaboratorioId(int laboratorioId) {
        this.laboratorioId = laboratorioId;
    }

    public int getExameId() {
        return exameId;
    }

    public void setExameId(int exameId) {
        this.exameId = exameId;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (int) laboratorioId;
        hash += (int) exameId;
        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 LaboratorioExameVOPK)) {
            return false;
        }
        LaboratorioExameVOPK other = (LaboratorioExameVOPK) object;
        if (this.laboratorioId != other.laboratorioId) {
            return false;
        }
        if (this.exameId != other.exameId) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.sismed.cadastros.java.LaboratorioExameVOPK[ laboratorioId=" + laboratorioId + ", exameId=" + exameId + " ]";
    }
    
}

Agora, estou tentando fazer uma consulta utilizando “SELECT LABORATORIOEXAME from LaboratorioExameVO as LABORATORIOEXAME” e me retorna erro:
Invalid path: ‘exame.nome’ [SELECT LABORATORIOEXAME from LaboratorioExameVO as LABORATORIOEXAME ORDER BY exame.nome ASC ].

Eu utilizei esta mesma consulta para tabelas que não tem relacionamento nenhum com outras tabelas e deu certo. Será que o problema está na consulta ou no mapeamento? Como eu trago todos os dados de uma classe que representa um relacionamento N:N? Alguém tem uma luz para me dar, por favor? Estou quebrando a cabeça com isso e não consigo caminhar!

Desde já agradeço!

Criado 14 de julho de 2011
Ultima resposta 18 de jul. de 2011
Respostas 6
Participantes 2