4
respostas

Salvar ArrayList - utilizando hibernate

boa tarde!

Tenho a seguinte situação:

Tenho um file que constrói um arrayIist com a quantidade 3k registros, há alguma possibilidade de hibernate iterar esse array e salvar todos os registros, falo isso, pois, estou correndo um forEach com e sem o parallelStream, porém a transação não é realizada com sucesso...

Alguém poderia me ajudar?

4 respostas

Qual código tentou até o momento?

Boa tarde!

Segue, tenho outras chamadas a esse método, porém, quando o volume do array é grande a

public class OmovimentoentradaRepository extends RepositoryHibernate<Omovimentoentrada, Serializable> {

    private static final long serialVersionUID = 1L;

    public OmovimentoentradaRepository() {
        super(new Omovimentoentrada());
        super.setS(HibernateUtil.getSessionFactory().openSession());
    }
}
package br.com.emk.repository;

import br.com.emk.util.HibernateUtil;
import java.io.Serializable;
import java.util.List;
import javax.persistence.Query;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author lg
 */
public abstract class RepositoryHibernate<T, ID extends Serializable> implements Serializable, IRepositoryHibernate<T> {

    private static final long serialVersionUID = 1L;
    private Session s;
    private Transaction t;
    private Query q;
    private Criteria c;

    private T entity;

    public RepositoryHibernate(T entity) {
        this.entity = entity;
    }

    public void save(T e) throws Exception {
        if (!s.isOpen() || getS() == null) {
            setS(HibernateUtil.getSessionFactory().openSession());
        }
        setT(getS().beginTransaction());
        getS().flush(); //implementado 
        getS().clear(); //implementado
        getS().save(e);
        getT().commit();
        getS().close();

    }

    public void update(T e) throws Exception {
        if (!s.isOpen() || getS() == null) {
            setS(HibernateUtil.getSessionFactory().openSession());
        }
        setT(getS().beginTransaction());
        getS().flush(); //implementado 
        getS().clear(); //implementado
        getS().merge(e);
        getT().commit();

        getS().close();

    }

    public void delete(T e) throws Exception {

        if (!s.isOpen() || getS() == null) {
            setS(HibernateUtil.getSessionFactory().openSession());
        }
        setT(getS().beginTransaction());
        getS().flush(); //implementado 
        getS().clear(); //implementado
        getS().delete(e);
        getT().commit();
        getS().close();
    }

    public List<T> findAll() throws Exception {
        if (!s.isOpen() || getS() == null) {
            setS(HibernateUtil.getSessionFactory().openSession());
        }
        @SuppressWarnings("unchecked")
        List<T> lista = getS().createCriteria(getEntity().getClass()).list();
        getS().close();
        return lista;
    }

    public List<T> findAllWithoutClose() throws Exception {
        setS(HibernateUtil.getSessionFactory().openSession());
        List<T> lista = getS().createCriteria(getEntity().getClass()).list();
        return lista;
    }

    public void closeSession() throws Exception {
        if (getS().isOpen()) {
            getS().close();
        }
    }

    public T findByCod(int cod) throws Exception {
        setS(HibernateUtil.getSessionFactory().openSession());
        T e = (T) getS().get(getEntity().getClass(), cod);
        getS().close();
        return e;
    }

    /**
     * @return the s
     */
    public Session getS() {
        return s;
    }

    /**
     * @param s the s to set
     */
    public void setS(Session s) {
        this.s = s;
    }

    /**
     * @return the t
     */
    public Transaction getT() {
        return t;
    }

    /**
     * @param t the t to set
     */
    public void setT(Transaction t) {
        this.t = t;
    }

    /**
     * @return the q
     */
    public Query getQ() {
        return q;
    }

    /**
     * @param q the q to set
     */
    public void setQ(Query q) {
        this.q = q;
    }

    /**
     * @return the c
     */
    public Criteria getC() {
        return c;
    }

    /**
     * @param c the c to set
     */
    public void setC(Criteria c) {
        this.c = c;
    }

    /**
     * @return the entity
     */
    public T getEntity() {
        return entity;
    }

    /**
     * @param entity the entity to set
     */
    public void setEntity(T entity) {
        this.entity = entity;
    }

}

package br.com.emk.entity;
// Generated 20/10/2018 21:05:37 by Hibernate Tools 4.3.1

import java.math.BigDecimal;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 * Omovimentoentrada generated by hbm2java
 */
@Entity
@Table(name = "OMOVIMENTOENTRADA",
        schema = "dbo",
        catalog = "EMK"
)
public class Omovimentoentrada implements java.io.Serializable {

    private int idmovimentoentrada;
    private Olayout olayout;
    private String codcompetencia;
    private int codempresa;
    private String codplano;
    private String tipoplano;
    private String matriculaexterna;
    private String coddependente;
    private Date datanascimento;
    private Date datainiciovigencia;
    private String tipolancamento;
    private String competencialancamento;
    private BigDecimal valor;
    private String nome;
    private String idademinima;
    private String codigolancamento;
    private String cpftitular;
    private String cpfdependente;
    private String nomeprestador;
    private Date datalancamento;
    private String reccreatedby;
    private Date reccreatedon;
    private String recmodifiedby;
    private Date recmodifiedon;
    private String nometitular;

    public Omovimentoentrada() {
    }


    @Id

    @Column(name = "IDMOVIMENTOENTRADA", nullable = false)
    public int getIdmovimentoentrada() {
        return this.idmovimentoentrada;
    }

    public void setIdmovimentoentrada(int idmovimentoentrada) {
        this.idmovimentoentrada = idmovimentoentrada;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "IDLAYOUT")
    public Olayout getOlayout() {
        return this.olayout;
    }

    public void setOlayout(Olayout olayout) {
        this.olayout = olayout;
    }

    @Column(name = "CODCOMPETENCIA", nullable = false, length = 6)
    public String getCodcompetencia() {
        return this.codcompetencia;
    }

    public void setCodcompetencia(String codcompetencia) {
        this.codcompetencia = codcompetencia;
    }

    @Column(name = "CODEMPRESA", nullable = false)
    public int getCodempresa() {
        return this.codempresa;
    }

    public void setCodempresa(int codempresa) {
        this.codempresa = codempresa;
    }

    @Column(name = "CODPLANO", length = 10)
    public String getCodplano() {
        return this.codplano;
    }

    public void setCodplano(String codplano) {
        this.codplano = codplano;
    }

    @Column(name = "TIPOPLANO", length = 10)
    public String getTipoplano() {
        return this.tipoplano;
    }

    public void setTipoplano(String tipoplano) {
        this.tipoplano = tipoplano;
    }

    @Column(name = "MATRICULAEXTERNA", length = 50)
    public String getMatriculaexterna() {
        return this.matriculaexterna;
    }

    public void setMatriculaexterna(String matriculaexterna) {
        this.matriculaexterna = matriculaexterna;
    }

    @Column(name = "CODDEPENDENTE", length = 5)
    public String getCoddependente() {
        return this.coddependente;
    }

    public void setCoddependente(String coddependente) {
        this.coddependente = coddependente;
    }

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "DATANASCIMENTO", length = 23)
    public Date getDatanascimento() {
        return this.datanascimento;
    }

    public void setDatanascimento(Date datanascimento) {
        this.datanascimento = datanascimento;
    }

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "DATAINICIOVIGENCIA", length = 23)
    public Date getDatainiciovigencia() {
        return this.datainiciovigencia;
    }

    public void setDatainiciovigencia(Date datainiciovigencia) {
        this.datainiciovigencia = datainiciovigencia;
    }

    @Column(name = "TIPOLANCAMENTO", length = 5)
    public String getTipolancamento() {
        return this.tipolancamento;
    }

    public void setTipolancamento(String tipolancamento) {
        this.tipolancamento = tipolancamento;
    }

    @Column(name = "COMPETENCIALANCAMENTO", length = 6)
    public String getCompetencialancamento() {
        return this.competencialancamento;
    }

    public void setCompetencialancamento(String competencialancamento) {
        this.competencialancamento = competencialancamento;
    }

    @Column(name = "VALOR", precision = 15, scale = 4)
    public BigDecimal getValor() {
        return this.valor;
    }

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

@Column(name = "NOME", length = 250)
    public String getNome() {
        return this.nome;
    }

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

    @Column(name = "IDADEMINIMA", length = 5)
    public String getIdademinima() {
        return this.idademinima;
    }

    public void setIdademinima(String idademinima) {
        this.idademinima = idademinima;
    }

    @Column(name = "CODIGOLANCAMENTO", length = 5)
    public String getCodigolancamento() {
        return this.codigolancamento;
    }

    public void setCodigolancamento(String codigolancamento) {
        this.codigolancamento = codigolancamento;
    }

    @Column(name = "CPFTITULAR", length = 11)
    public String getCpftitular() {
        return this.cpftitular;
    }

    public void setCpftitular(String cpftitular) {
        this.cpftitular = cpftitular;
    }

    @Column(name = "CPFDEPENDENTE", length = 11)
    public String getCpfdependente() {
        return this.cpfdependente;
    }

    public void setCpfdependente(String cpfdependente) {
        this.cpfdependente = cpfdependente;
    }

    @Column(name = "NOMEPRESTADOR", length = 250)
    public String getNomeprestador() {
        return this.nomeprestador;
    }

    public void setNomeprestador(String nomeprestador) {
        this.nomeprestador = nomeprestador;
    }

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "DATALANCAMENTO", length = 23)
    public Date getDatalancamento() {
        return this.datalancamento;
    }

    public void setDatalancamento(Date datalancamento) {
        this.datalancamento = datalancamento;
    }

    @Column(name = "RECCREATEDBY", length = 50)
    public String getReccreatedby() {
        return this.reccreatedby;
    }

    public void setReccreatedby(String reccreatedby) {
        this.reccreatedby = reccreatedby;
    }

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "RECCREATEDON", length = 23)
    public Date getReccreatedon() {
        return this.reccreatedon;
    }

    public void setReccreatedon(Date reccreatedon) {
        this.reccreatedon = reccreatedon;
    }

    @Column(name = "RECMODIFIEDBY", length = 50)
    public String getRecmodifiedby() {
        return this.recmodifiedby;
    }

    public void setRecmodifiedby(String recmodifiedby) {
        this.recmodifiedby = recmodifiedby;
    }

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "RECMODIFIEDON", length = 23)
    public Date getRecmodifiedon() {
        return this.recmodifiedon;
    }

    public void setRecmodifiedon(Date recmodifiedon) {
        this.recmodifiedon = recmodifiedon;
    }

    @Column(name = "NOMETITULAR", length = 250)
    public String getNometitular() {
        return nometitular;
    }

    public void setNometitular(String nometitular) {
        this.nometitular = nometitular;
    }

}

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software