sz.fcv.core.db.HQLExecuter.java Source code

Java tutorial

Introduction

Here is the source code for sz.fcv.core.db.HQLExecuter.java

Source

/*
 * Copyright (C) 2015 stefanoziller
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package sz.fcv.core.db;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.io.Serializable;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.exception.ConstraintViolationException;
import org.hibernate.type.Type;

/**
 * Implementa alcune funzioni per l'esecuzione di query tramite Hibernate.
 * 
 * @author stefanoziller
 */
public class HQLExecuter {

    private static String openSQLFile(String fileName) throws FileNotFoundException, IOException {
        RandomAccessFile raf = new RandomAccessFile("queries/" + fileName + ".sql", "r");

        String hql = "";
        for (String line; (line = raf.readLine()) != null;) {
            hql += line + "\n";
        }

        return hql;
    }

    /**
     * Inizializza una nuova sessione.
     * 
     * @return 
     */
    public static Session createSession() {
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        return session;
    }

    /**
     * Chiude una sessione e salva le modifiche al db.
     * 
     * @param session 
     */
    public static void commitSession(Session session) {
        session.getTransaction().commit();
        session.close();
    }

    /**
     * Chiude una sessione ed annulla le modifiche al db.
     * 
     * @param session 
     */
    public static void rollbackSession(Session session) {
        session.getTransaction().rollback();
        session.close();
    }

    /**
     * Chiamata dalla classe figlia per eseguire una query hql
     * 
     * @param s Sessione, null per nuova sessione
     * @param hql 
     * @param os
     * @param types 
     * 
     * @throws java.io.FileNotFoundException
     * 
     * @return lista oggetti risultanti
     */
    public static List executeHQLQuery(Session s, String hql, Object[] os, Type[] types)
            throws HibernateException, FileNotFoundException, IOException {

        Session session;
        if (s != null) {
            session = s;
        } else {
            session = createSession();
        }

        Query q = session.createQuery(openSQLFile(hql));

        // System.out.println("Esecuzione query: " + q);

        if (os != null) {
            q.setParameters(os, types);
        }

        List resultList = q.list();

        if (s == null) {
            commitSession(session);
        }

        return resultList;
    }

    /**
     * Esecuzione di una query SQL
     * 
     * @param s
     * @param sql
     * @param os
     * @param types
     * @return
     * @throws HibernateException
     * @throws FileNotFoundException
     * @throws IOException 
     */
    public static List executeSQLQuery(Session s, String sql, Object[] os, Type[] types)
            throws HibernateException, FileNotFoundException, IOException {
        Session session;
        if (s != null) {
            session = s;
        } else {
            session = createSession();
        }

        Query q = session.createSQLQuery(openSQLFile(sql));

        if (os != null) {
            q.setParameters(os, types);
        }

        List resultList = q.list();

        if (s == null) {
            commitSession(session);
        }

        return resultList;
    }

    /**
     * Chiamata dalla classe figlia per eseguire una query hql
     * 
     * @param hql 
     * @param os
     * @param types 
     */
    public static void executeHQLUpdate(Session s, String hql, Object[] os, Type[] types)
            throws ConstraintViolationException, HibernateException, FileNotFoundException, IOException {

        Session session;
        if (s != null) {
            session = s;
        } else {
            session = createSession();
        }

        Query q = session.createQuery(openSQLFile(hql));
        q.setParameters(os, types);

        q.executeUpdate();
        if (s == null) {
            commitSession(session);
        }
    }

    /**
     * Salvataggio
     * 
     * @param s Sessione
     * @param c Oggetto da salvare
     * @return Codice nuovo record
     */
    public static int executeHQLSave(Session s, Serializable c)
            throws ConstraintViolationException, HibernateException {

        int code = -1;

        Session session;
        if (s != null) {
            session = s;
        } else {
            session = createSession();
        }

        session.save(c);

        List codeList = session.createSQLQuery("select currval('genera_codice')").list();
        if (codeList != null && codeList.size() > 0) {
            code = Integer.parseInt(codeList.get(0).toString());
        }

        if (s == null) {
            commitSession(session);
        }

        return code;
    }

    /**
     * 
     * @param hql
     * @param os
     * @param types 
     */
    public static void executeHQLDelete(Session s, String hql, Object[] os, Type[] types)
            throws HibernateException, FileNotFoundException, IOException {
        Session session;
        if (s != null) {
            session = s;
        } else {
            session = createSession();
        }

        Query q = session.createQuery(openSQLFile(hql));
        q.setParameters(os, types);

        q.executeUpdate();
        if (s == null) {
            commitSession(session);
        }
    }

    /**
     * 
     * @param s
     * @param sql
     * @param os
     * @param types
     * @throws HibernateException
     * @throws FileNotFoundException
     * @throws IOException 
     */
    public static void executeSQLUpdate(Session s, String sql, Object[] os, Type[] types)
            throws HibernateException, FileNotFoundException, IOException {

        Session session;
        if (s != null) {
            session = s;
        } else {
            session = createSession();
        }

        Query q = session.createSQLQuery(openSQLFile(sql));
        q.setParameters(os, types);

        q.executeUpdate();
        if (s == null) {
            commitSession(session);
        }
    }
}