ManyToOne Hibernate

1 resposta
E

Olá pessoal,

Estou tentando fazer relacionamento com hibernate ManyToOne para inserção.

As tabelas ja estão criadas, e gerei as Classes( com Annotations) e os XMLs, automático a partir das gerações de POJOs no NetBeans.

O CRUD de cadastros esta funcionando. O que preciso implementar é entrada de nota onde terá uma tabela de cabeçalho da nota e outra tabela de ítens dessa nota(ManyToOne).

1 Resposta

E

Cabeçalho NF ********

package modelo;
// Generated 04/05/2016 11:41:27 by Hibernate Tools 3.6.0

import java.util.Date;

import java.util.HashSet;

import java.util.Set;

import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.FetchType;

import javax.persistence.GeneratedValue;

import static javax.persistence.GenerationType.IDENTITY;

import <a href="http://javax.persistence.Id">javax.persistence.Id</a>;

import javax.persistence.OneToMany;

import javax.persistence.Table;

import javax.persistence.Temporal;

import javax.persistence.TemporalType;

/**

  • Nfent generated by hbm2java
    
    */
    
    @Entity
    
    @Table(name=nfent
    
    ,catalog=galpres
    
    )
    
    public class Nfent  implements java.io.Serializable {
    
    private Integer numtransent;
    
    private int numnfent;
    
    private Date dtent;
    
    private Date dtselo;
    
    private Date dtemissao;
    
    private Date dtsaida;
    
    private Double vltotal;
    
    private String tipoentrada;
    
    private String serie;
    
    private String especie;
    
    private Integer codfilial;
    
    private Integer codfornec;
    
    private Date dtcancel;
    
    private Set nfmovs = new HashSet(0);
    

    public Nfent() {
    }

    public Nfent(int numnfent, Date dtent) {
    
    this.numnfent = numnfent;
    
    this.dtent = dtent;
    
    }
    
    public Nfent(int numnfent, Date dtent, Date dtselo, Date dtemissao, Date dtsaida, Double vltotal, String tipoentrada, String serie, String especie, Integer codfilial, Integer codfornec, Date dtcancel, Set nfmovs) {
    
    this.numnfent = numnfent;
    
    this.dtent = dtent;
    
    this.dtselo = dtselo;
    
    this.dtemissao = dtemissao;
    
    this.dtsaida = dtsaida;
    
    this.vltotal = vltotal;
    
    this.tipoentrada = tipoentrada;
    
    this.serie = serie;
    
    this.especie = especie;
    
    this.codfilial = codfilial;
    
    this.codfornec = codfornec;
    
    this.dtcancel = dtcancel;
    
    this.nfmovs = nfmovs;
    
    }
    

    @Id @GeneratedValue(strategy=IDENTITY)

    @Column(name=numtransent, unique=true, nullable=false)
    
    public Integer getNumtransent() {
    
    return this.numtransent;
    
    }
    
    public void setNumtransent(Integer numtransent) {
    
    this.numtransent = numtransent;
    
    }
    
    @Column(name=numnfent, nullable=false)
    
    public int getNumnfent() {
    
    return this.numnfent;
    
    }
    
    public void setNumnfent(int numnfent) {
    
    this.numnfent = numnfent;
    
    }
    
    @Temporal(TemporalType.DATE)
    
    @Column(name=dtent, nullable=false, length=10)
    
    public Date getDtent() {
    
    return this.dtent;
    
    }
    
    public void setDtent(Date dtent) {
    
    this.dtent = dtent;
    
    }
    
    @Temporal(TemporalType.DATE)
    
    @Column(name=dtselo, length=10)
    
    public Date getDtselo() {
    
    return this.dtselo;
    
    }
    
    public void setDtselo(Date dtselo) {
    
    this.dtselo = dtselo;
    
    }
    
    @Temporal(TemporalType.DATE)
    
    @Column(name=dtemissao, length=10)
    
    public Date getDtemissao() {
    
    return this.dtemissao;
    
    }
    
    public void setDtemissao(Date dtemissao) {
    
    this.dtemissao = dtemissao;
    
    }
    
    @Temporal(TemporalType.DATE)
    
    @Column(name=dtsaida, length=10)
    
    public Date getDtsaida() {
    
    return this.dtsaida;
    
    }
    
    public void setDtsaida(Date dtsaida) {
    
    this.dtsaida = dtsaida;
    
    }
    
    @Column(name=vltotal, precision=22, scale=0)
    
    public Double getVltotal() {
    
    return this.vltotal;
    
    }
    
    public void setVltotal(Double vltotal) {
    
    this.vltotal = vltotal;
    
    }
    
    @Column(name=tipoentrada, length=45)
    
    public String getTipoentrada() {
    
    return this.tipoentrada;
    
    }
    
    public void setTipoentrada(String tipoentrada) {
    
    this.tipoentrada = tipoentrada;
    
    }
    
    @Column(name=serie, length=5)
    
    public String getSerie() {
    
    return this.serie;
    
    }
    
    public void setSerie(String serie) {
    
    this.serie = serie;
    
    }
    
    @Column(name=especie, length=5)
    
    public String getEspecie() {
    
    return this.especie;
    
    }
    
    public void setEspecie(String especie) {
    
    this.especie = especie;
    
    }
    
    @Column(name=codfilial)
    
    public Integer getCodfilial() {
    
    return this.codfilial;
    
    }
    
    public void setCodfilial(Integer codfilial) {
    
    this.codfilial = codfilial;
    
    }
    
    @Column(name=codfornec)
    
    public Integer getCodfornec() {
    
    return this.codfornec;
    
    }
    
    public void setCodfornec(Integer codfornec) {
    
    this.codfornec = codfornec;
    
    }
    
    @Temporal(TemporalType.DATE)
    
    @Column(name=dtcancel, length=10)
    
    public Date getDtcancel() {
    
    return this.dtcancel;
    
    }
    
    public void setDtcancel(Date dtcancel) {
    
    this.dtcancel = dtcancel;
    
    }
    
@OneToMany(fetch=FetchType.LAZY, mappedBy=nfent)

public Set getNfmovs() {

return this.nfmovs;

}
public void setNfmovs(Set nfmovs) {
    this.nfmovs = nfmovs;
}

}


Itens NF.

package modelo;
// Generated 04/05/2016 11:41:27 by Hibernate Tools 3.6.0

import javax.persistence.Column;
import javax.persistence.Embeddable;

/**

  • NfmovId generated by hbm2java
    */
    @Embeddable
    public class NfmovId implements java.io.Serializable {

    private int numtransacao;
    
    private Integer numnota;
    
    private int item;
    
    private String descricao;
    
    private Double vltotalitens;
    
    private Integer qtunit;
    
    private String vlunit;
    
    private String tipomov;
    

    public NfmovId() {
    }

    public NfmovId(int numtransacao, int item, String descricao) {
    
    this.numtransacao = numtransacao;
    
    this.item = item;
    
    this.descricao = descricao;
    
    }
    
    public NfmovId(int numtransacao, Integer numnota, int item, String descricao, Double vltotalitens, Integer qtunit, String vlunit, String tipomov) {
    
    this.numtransacao = numtransacao;
    
    this.numnota = numnota;
    
    this.item = item;
    
    this.descricao = descricao;
    
    this.vltotalitens = vltotalitens;
    
    this.qtunit = qtunit;
    
    this.vlunit = vlunit;
    
    this.tipomov = tipomov;
    
    }
    
    @Column(name=numtransacao, nullable=false)
    
    public int getNumtransacao() {
    
    return this.numtransacao;
    
    }
    
    public void setNumtransacao(int numtransacao) {
    
    this.numtransacao = numtransacao;
    
    }
    
    @Column(name=numnota)
    
    public Integer getNumnota() {
    
    return this.numnota;
    
    }
    
    public void setNumnota(Integer numnota) {
    
    this.numnota = numnota;
    
    }
    
    @Column(name=item, nullable=false)
    
    public int getItem() {
    
    return this.item;
    
    }
    
    public void setItem(int item) {
    
    this.item = item;
    
    }
    
    @Column(name=descricao, nullable=false, length=100)
    
    public String getDescricao() {
    
    return this.descricao;
    
    }
    
    public void setDescricao(String descricao) {
    
    this.descricao = descricao;
    
    }
    
    @Column(name=vltotalitens, precision=22, scale=0)
    
    public Double getVltotalitens() {
    
    return this.vltotalitens;
    
    }
    
    public void setVltotalitens(Double vltotalitens) {
    
    this.vltotalitens = vltotalitens;
    
    }
    
    @Column(name=qtunit)
    
    public Integer getQtunit() {
    
    return this.qtunit;
    
    }
    
    public void setQtunit(Integer qtunit) {
    
    this.qtunit = qtunit;
    
    }
    
    @Column(name=vlunit, length=45)
    
    public String getVlunit() {
    
    return this.vlunit;
    
    }
    
    public void setVlunit(String vlunit) {
    
    this.vlunit = vlunit;
    
    }
    
    @Column(name=tipomov, length=3)
    
    public String getTipomov() {
    
    return this.tipomov;
    
    }
    
    public void setTipomov(String tipomov) {
    
    this.tipomov = tipomov;
    
    }
    
    public boolean equals(Object other) {
    
    if ( (this == other ) ) return true;
    
    if ( (other == null ) ) return false;
    
    if ( !(other instanceof NfmovId) ) return false;
    
    NfmovId castOther = ( NfmovId ) other;
    
    return (this.getNumtransacao()==castOther.getNumtransacao())
    
&& ( (this.getNumnota()==castOther.getNumnota()) || ( this.getNumnota()!=null && castOther.getNumnota()!=null && this.getNumnota().equals(castOther.getNumnota()) ) )

&& (this.getItem()==castOther.getItem())

&& ( (this.getDescricao()==castOther.getDescricao()) || ( this.getDescricao()!=null && castOther.getDescricao()!=null && this.getDescricao().equals(castOther.getDescricao()) ) )

&& ( (this.getVltotalitens()==castOther.getVltotalitens()) || ( this.getVltotalitens()!=null && castOther.getVltotalitens()!=null && this.getVltotalitens().equals(castOther.getVltotalitens()) ) )

&& ( (this.getQtunit()==castOther.getQtunit()) || ( this.getQtunit()!=null && castOther.getQtunit()!=null && this.getQtunit().equals(castOther.getQtunit()) ) )

&& ( (this.getVlunit()==castOther.getVlunit()) || ( this.getVlunit()!=null && castOther.getVlunit()!=null && this.getVlunit().equals(castOther.getVlunit()) ) )

&& ( (this.getTipomov()==castOther.getTipomov()) || ( this.getTipomov()!=null && castOther.getTipomov()!=null && this.getTipomov().equals(castOther.getTipomov()) ) );

}

public int hashCode() {
int result = 17;

result = 37 * result + this.getNumtransacao();
     result = 37 * result + ( getNumnota() == null ? 0 : this.getNumnota().hashCode() );
     result = 37 * result + this.getItem();
     result = 37 * result + ( getDescricao() == null ? 0 : this.getDescricao().hashCode() );
     result = 37 * result + ( getVltotalitens() == null ? 0 : this.getVltotalitens().hashCode() );
     result = 37 * result + ( getQtunit() == null ? 0 : this.getQtunit().hashCode() );
     result = 37 * result + ( getVlunit() == null ? 0 : this.getVlunit().hashCode() );
     result = 37 * result + ( getTipomov() == null ? 0 : this.getTipomov().hashCode() );
     return result;

}

}


Relacionamento >> Na geração dos POJOs Criou essa classe

package modelo;
// Generated 04/05/2016 11:41:27 by Hibernate Tools 3.6.0

import javax.persistence.AttributeOverride;

import javax.persistence.AttributeOverrides;

import javax.persistence.Column;

import javax.persistence.EmbeddedId;

import javax.persistence.Entity;

import javax.persistence.FetchType;

import javax.persistence.JoinColumn;

import javax.persistence.ManyToOne;

import javax.persistence.Table;

/**

  • Nfmov generated by hbm2java
    
    */
    
    @Entity
    
    @Table(name=nfmov
    
    ,catalog=galpres
    
    )
    
    public class Nfmov  implements java.io.Serializable {
    

    private NfmovId id;
    private Nfent nfent;

    public Nfmov() {
    }

    public Nfmov(NfmovId id, Nfent nfent) {
    
    <a href="http://this.id">this.id</a> = id;
    
    this.nfent = nfent;
    
    }
    

    @EmbeddedId

    @AttributeOverrides( {
    
    @AttributeOverride(name=“numtransacao”, column=@Column(name=“numtransacao”, nullable=false) ),
    
    @AttributeOverride(name=“numnota”, column=@Column(name=“numnota”) ),
    
    @AttributeOverride(name=“item”, column=@Column(name=“item”, nullable=false) ),
    
    @AttributeOverride(name=“descricao”, column=@Column(name=“descricao”, nullable=false, length=100) ),
    
    @AttributeOverride(name=“vltotalitens”, column=@Column(name=“vltotalitens”, precision=22, scale=0) ),
    
    @AttributeOverride(name=“qtunit”, column=@Column(name=“qtunit”) ),
    
    @AttributeOverride(name=“vlunit”, column=@Column(name=“vlunit”, length=45) ),
    
    @AttributeOverride(name=“tipomov”, column=@Column(name=“tipomov”, length=3) ) } )
    
    public NfmovId getId() {
    
    return <a href="http://this.id">this.id</a>;
    
    }
    
    public void setId(NfmovId id) {
    
    <a href="http://this.id">this.id</a> = id;
    
    }
    
@ManyToOne(fetch=FetchType.LAZY)

@JoinColumn(name=numtransacao, nullable=false, insertable=false, updatable=false)

public Nfent getNfent() {

return this.nfent;

}
public void setNfent(Nfent nfent) {
    this.nfent = nfent;
}

}

Criado 4 de maio de 2016
Ultima resposta 4 de mai. de 2016
Respostas 1
Participantes 1