fr.utbm.core.dao.AlertDao.java Source code

Java tutorial

Introduction

Here is the source code for fr.utbm.core.dao.AlertDao.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 fr.utbm.core.dao;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;

import fr.utbm.core.entity.Alert;
import fr.utbm.core.tools.HibernateUtil;

/**
 *
 * @author Julien
 */
public class AlertDao {
    public void registerAlert(Alert a) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            session.beginTransaction();
            session.persist(a);
            session.getTransaction().commit();
        } catch (HibernateException he) {
            he.printStackTrace();
            if (session.getTransaction() != null) {
                try {
                    session.getTransaction().rollback();
                } catch (HibernateException he2) {
                    he2.printStackTrace();
                }
            }
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

    public Alert getAlertById(String id) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        Alert a = (Alert) session.get(Alert.class, id);
        session.close();
        return a;
    }

    public void deleteAlertById(String id) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            session.beginTransaction();
            Alert a = (Alert) session.get(Alert.class, id);
            session.delete(a);
            session.getTransaction().commit();
        } catch (HibernateException he) {
            he.printStackTrace();
            if (session.getTransaction() != null) {
                try {
                    session.getTransaction().rollback();
                } catch (HibernateException he2) {
                    he2.printStackTrace();
                }
            }
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }

    public List<Alert> getAllAlerts() {
        List<Alert> res = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        Query query = session.createQuery("from fr.utbm.core.entity.Alert");
        res = query.list();
        session.close();
        return res;
    }
}