com.controllers.OrdersFacade.java Source code

Java tutorial

Introduction

Here is the source code for com.controllers.OrdersFacade.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.controllers;

import com.pojo.Orders;
import static com.util.NewHibernateUtil.getSessionFactory;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author Khalil Sharkawi
 */
@Stateless
public class OrdersFacade extends AbstractFacade<Orders> {

    public OrdersFacade() {
        super(Orders.class);
    }

    @Override
    public void create(Orders entity) {
        Transaction trs = null;
        Session session = getSessionFactory().openSession();
        try {
            trs = session.beginTransaction();
            session.save(entity);
            session.getTransaction().commit();
        } catch (RuntimeException e) {
            if (trs != null) {
                trs.rollback();
            }
        } finally {
            session.flush();
            session.close();
        }
    }

    @Override
    public void edit(Orders entity) {
        Transaction trs = null;
        Session session = getSessionFactory().openSession();
        try {
            trs = session.beginTransaction();
            session.saveOrUpdate(entity);
            session.getTransaction().commit();
        } catch (RuntimeException e) {
            if (trs != null) {
                trs.rollback();
            }
        } finally {
            session.flush();
            session.close();
        }

    }

    @Override
    public void remove(Orders entity) {
        Transaction trs = null;
        Session session = getSessionFactory().openSession();
        try {
            trs = session.beginTransaction();
            session.delete(entity);
            session.getTransaction().commit();
        } catch (RuntimeException e) {
            if (trs != null) {
                trs.rollback();
            }
        } finally {
            session.flush();
            session.close();
        }
    }

    @Override
    public Orders find(Object id) {
        Orders myObject = null;
        Transaction trs = null;
        Session session = getSessionFactory().openSession();
        try {
            myObject = (Orders) session.load(Orders.class, new Integer((int) id));

        } catch (RuntimeException e) {
            if (trs != null) {
                session.close();
                return null;
            }
        } finally {
            session.flush();
            session.close();
            return myObject;
        }
    }

    @Override
    public List<Orders> findAll() {
        Session session = getSessionFactory().openSession();
        List<Orders> l = new ArrayList<>(session.createQuery("FROM Orders").list());

        session.flush();
        session.close();
        return l;
    }

    @Override
    public List<Orders> findAll(int s) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public Orders findByName(String name) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

}