com.model.manegers.User_maneger.java Source code

Java tutorial

Introduction

Here is the source code for com.model.manegers.User_maneger.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.model.manegers;

import com.util.HibernateUtil;
import static com.util.HibernateUtil.getSessionFactory;
import com.util.Users;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author Khalil Sharkawi
 */
public class User_maneger {

    public static List<Users> getUsers() {
        Session session = getSessionFactory().openSession();
        List<Users> l = session.createQuery("FROM Users").list();
        //        session.close();
        return l;
    }

    public static void AddUser(Users p) {
        Transaction trs = null;
        Session session = getSessionFactory().openSession();
        try {
            trs = session.beginTransaction();
            session.save(p);
            session.getTransaction().commit();
        } catch (RuntimeException e) {
            if (trs != null) {
                trs.rollback();
            }
        } finally {
            session.flush();
            session.close();
        }

    }

    public static void DeleteUser(int id) {

        Users myObject;
        Transaction trs = null;
        Session session = getSessionFactory().openSession();
        try {
            trs = session.beginTransaction();
            myObject = (Users) session.load(Users.class, new Integer((int) id));
            session.delete(myObject);
            session.getTransaction().commit();
        } catch (RuntimeException e) {
            if (trs != null) {
                trs.rollback();
            }
        } finally {
            session.flush();
            session.close();
        }
    }

    public static void updateUser(Users pro) {
        Transaction trns = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            trns = session.beginTransaction();
            session.saveOrUpdate(pro);
            session.getTransaction().commit();
        } catch (RuntimeException e) {
            if (trns != null) {
                trns.rollback();
            }
            e.printStackTrace();
        } finally {
            session.flush();
            session.close();
        }
    }

    public static Users getByid(int id) {
        Users myObject = null;
        Transaction trs = null;
        Session session = getSessionFactory().openSession();
        try {
            //            trs = session.beginTransaction();
            myObject = (Users) session.load(Users.class, new Integer((int) id));
            //            session.delete(myObject);
            //            session.getTransaction().commit();
        } catch (RuntimeException e) {
            if (trs != null) {
                //                trs.rollback();
            }
        } finally {
            session.flush();
            session.close();
            return myObject;
        }
    }
}