Duvidas sobre Mapeamento

4 respostas
J

Ola a todos

Estou com a seguinte duvida, possuo tres tabelas que desejo mapear p/ usar com o Hibernate, e não sei por onde devo começar.As tabelas são descritas abaixo:


PRODUTO
IDPRD…
NOME…
DESC…


PRODUTOLOC
IDPRD…
IDLOCAL…
ESTOQUE…


LOCAL
IDLOCAL.

4 Respostas

J

Achei este Material:
http://boris.kirzner.info/blog/archives/2008/07/19/hibernate-annotations-the-many-to-many-association-with-composite-key/
fiz o exemplo e criou 2 pk na tabela de Assoc.

Se tiverem mais algum outro material ou recomendação de livro, por favor me mandem:

arthurminarini

na PRODUTOLOC os campos IDPRD e IDLOCAL alem de serem FK são PK também?

J

sim

arthurminarini

produto

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "PRODUTO")
public class Produto implements Serializable{

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)//para cada banco tem uma forma de gerar chave esta é para para sqlServer
    @Column(name = "IDPRD", columnDefinition = "INT")
    private Integer idPrd;

    @Column(name = "NOME", length = 50)
    private String nome;

    @Column(name = "DESCRICAO", length = 50)
    private String descricao;//desc é palavra reservada do sql evite usar

    public String getDescricao() {
        return descricao;
    }

    public void setDescricao(String descricao) {
        this.descricao = descricao;
    }

    public Integer getIdPrd() {
        return idPrd;
    }

    public void setIdPrd(Integer idPrd) {
        this.idPrd = idPrd;
    }

    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 Produto other = (Produto) obj;
        if (this.idPrd != other.idPrd && (this.idPrd == null || !this.idPrd.equals(other.idPrd))) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 79 * hash + (this.idPrd != null ? this.idPrd.hashCode() : 0);
        return hash;
    }

}

local

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "LOCAL")
public class Local implements Serializable{

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)//para cada banco tem uma forma de gerar chave esta é para para sqlServer
    @Column(name = "IDLOCAL", columnDefinition = "INT")
    private Integer idLocal;

    public Integer getIdLocal() {
        return idLocal;
    }

    public void setIdLocal(Integer idLocal) {
        this.idLocal = idLocal;
    }

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

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 31 * hash + (this.idLocal != null ? this.idLocal.hashCode() : 0);
        return hash;
    }

}

produtoloc

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Table;

@Entity
@Table(name = "PRODUTOLOC")
public class ProdutoLoc implements Serializable{

    @EmbeddedId
    private ChaveProdutoLoc codProdutoLoc;

    @Column(name = "ESTOQUE")
    private Integer estoque;

    public ChaveProdutoLoc getCodProdutoLoc() {
        return codProdutoLoc;
    }

    public void setCodProdutoLoc(ChaveProdutoLoc codProdutoLoc) {
        this.codProdutoLoc = codProdutoLoc;
    }

    public Integer getEstoque() {
        return estoque;
    }

    public void setEstoque(Integer estoque) {
        this.estoque = estoque;
    }

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

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

}

chave da tabela acima

import java.io.Serializable;
import javax.persistence.Embeddable;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

@Embeddable
public class ChaveProdutoLoc implements Serializable{

    @ManyToOne(fetch=FetchType.EAGER)
    @JoinColumn(name = "IDPRD")
    private Produto idPrd;

    @ManyToOne(fetch=FetchType.EAGER)
    @JoinColumn(name = "IDLOCAL")
    private Local idLocal;

    public ChaveProdutoLoc(Produto idPrd, Local idLocal) {
        this.idPrd = idPrd;
        this.idLocal = idLocal;
    }

    public ChaveProdutoLoc() {
    }

    public Local getIdLocal() {
        return idLocal;
    }

    public void setIdLocal(Local idLocal) {
        this.idLocal = idLocal;
    }

    public Produto getIdPrd() {
        return idPrd;
    }

    public void setIdPrd(Produto idPrd) {
        this.idPrd = idPrd;
    }

}

no seu hibernateutil
adicione suas classes anotadas pela classe AnnotationConfiguration e pelo metodo addAnnotatedClass!!

:wink:

Criado 17 de novembro de 2010
Ultima resposta 18 de nov. de 2010
Respostas 4
Participantes 2