com.demo.impl.CorredorImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.demo.impl.CorredorImpl.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.demo.impl;

import com.demo.dao.CorredorDAO;
import com.demo.model.Corredor;
import com.demo.util.HibernateUtil;
import com.demo.util.LogSistema;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author brionvega
 */
public class CorredorImpl implements Serializable, CorredorDAO {

    @Override
    public List<Corredor> obtenerCorredores() {
        List<Corredor> corr = new ArrayList();

        try {
            Session session = HibernateUtil.getSessionFactory().openSession();
            Transaction tx = session.beginTransaction();
            corr = session.createSQLQuery("select * from corredor;").addEntity(Corredor.class).list();
            tx.commit();
        } catch (HibernateException j) {
            System.out.println("Error Corredor");
            System.out.println(j.getMessage());
        }
        return corr;
    }

    @Override
    public boolean guardarCorredor(Corredor corr) {
        boolean hecho = false;
        try {
            System.out.println("Se guardo El corredor");
            Session sesion = HibernateUtil.getSessionFactory().openSession();
            Transaction tx = sesion.beginTransaction();
            sesion.save(corr);
            tx.commit();
            hecho = true;

        } catch (HibernateException e) {
            LogSistema.guardarlog(
                    this.getClass().getName() + " Method: guardarCorredor, Exception: " + e.getMessage());
            System.out.println("Error CorredorImpl " + e.getMessage());

        }

        return hecho;
    }

    @Override
    public boolean ActualizarCorredor(Corredor corr) {
        boolean hecho = false;
        try {
            System.out.println("Se actualizo el corredor");
            Session sesion = HibernateUtil.getSessionFactory().openSession();
            Transaction tx = sesion.beginTransaction();
            sesion.update(corr);
            tx.commit();
            hecho = true;

        } catch (HibernateException e) {
            LogSistema.guardarlog(
                    this.getClass().getName() + " Method: ActualizarCorredor, se realizo marcaje semiautomatico");
            System.out.println("Error CorredorImpl " + e.getMessage());

        }

        return hecho;
    }

}