com.erickrgb.test.HibernateManager.java Source code

Java tutorial

Introduction

Here is the source code for com.erickrgb.test.HibernateManager.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.erickrgb.test;

import com.erickrgb.hiberbeans.Alumno;
import java.util.List;
import java.util.Iterator;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

/**
 *
 * @author Erick RGB
 */
public class HibernateManager {

    private static SessionFactory factory;

    public static void main(String[] args) {
        try {
            factory = new Configuration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            System.err.println("Failed to create sessionFactory object." + ex);
            throw new ExceptionInInitializerError(ex);
        }
        HibernateManager ME = new HibernateManager();

        /* Add few employee records in database */
        Integer empID1 = ME.addAlumno("Zara", "Ali", "Lois");
        Integer empID2 = ME.addAlumno("Daisy", "Das", "Paca");
        Integer empID3 = ME.addAlumno("John", "Paul", "Fer");

        /* List down all the employees */
        ME.listAlumnos();

        /* Update employee's records */
        ME.updateAlumno(empID1, "Juanelique");

        /* Delete an employee from the database */
        ME.deleteAlumno(empID2);

        /* List down new list of the employees */
        ME.listAlumnos();
    }
    /* Method to CREATE an employee in the database */

    public Integer addAlumno(String fname, String lname, String nombre) {
        Session session = factory.openSession();
        Transaction tx = null;
        Integer employeeID = null;
        try {
            tx = session.beginTransaction();
            Alumno employee = new Alumno(fname, lname, nombre);
            employeeID = (Integer) session.save(employee);
            tx.commit();
        } catch (HibernateException e) {
            if (tx != null) {
                tx.rollback();
            }
            e.printStackTrace();
        } finally {
            session.close();
        }
        return employeeID;
    }
    /* Method to  READ all the employees */

    public void listAlumnos() {
        Session session = factory.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            List employees = session.createQuery("FROM Alumno").list();
            for (Iterator iterator = employees.iterator(); iterator.hasNext();) {
                Alumno employee = (Alumno) iterator.next();
                System.out.print("First Name: " + employee.getApPaterno());
                System.out.print("  Last Name: " + employee.getApMaterno());
                System.out.println("  Salary: " + employee.getNombre());
            }
            tx.commit();
        } catch (HibernateException e) {
            if (tx != null) {
                tx.rollback();
            }
            e.printStackTrace();
        } finally {
            session.close();
        }
    }
    /* Method to UPDATE salary for an employee */

    public void updateAlumno(Integer AlumnoID, String nombre) {
        Session session = factory.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            Alumno employee = (Alumno) session.get(Alumno.class, AlumnoID);
            employee.setNombre(nombre);
            session.update(employee);
            tx.commit();
        } catch (HibernateException e) {
            if (tx != null) {
                tx.rollback();
            }
            e.printStackTrace();
        } finally {
            session.close();
        }
    }
    /* Method to DELETE an employee from the records */

    public void deleteAlumno(Integer AlumnoID) {
        Session session = factory.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            Alumno employee = (Alumno) session.get(Alumno.class, AlumnoID);
            session.delete(employee);
            tx.commit();
        } catch (HibernateException e) {
            if (tx != null) {
                tx.rollback();
            }
            e.printStackTrace();
        } finally {
            session.close();
        }
    }
}