/** * Created on 17 Jan. 2007 * @author */ package com.medias.utils.hibernate; import java.io.Serializable; import java.util.List; import org.hibernate.*; import org.hibernate.criterion.Order; import org.hibernate.criterion.Restrictions; /** * * Accès et échanges avec la base de données */ public class CommunicationBD { private static Transaction tx = null; private static Session session = null; /** * Ouverture de la session */ public static void open() throws HibernateException { try { session = SessionManager.currentSession(); tx = session.beginTransaction(); } catch (HibernateException e) { e.printStackTrace(); throw (e); } } /** * Fermeture de la session */ public static void close() throws HibernateException { try { tx.commit(); SessionManager.closeSession(); } catch (HibernateException e) { e.printStackTrace(); throw (e); } } /** * Requête de sélection sur une table * SELECT * FROM table; * Renvoie la liste des tuples de la table */ public static List getAllList(Class aClass) throws HibernateException { List results = null; try { //tx = session.beginTransaction(); results = session.createCriteria(aClass).list(); //tx.commit(); } catch (HibernateException e) { e.printStackTrace(); throw (e); } return results; } /** * Requête de sélection ordonnée sur une table * SELECT * FROM table ORDER BY colonne; * Renvoie la liste des tuples de la table */ public static List getOrderedList(Class aClass, String column) throws HibernateException { List results = null; try { //tx = session.beginTransaction(); results = session.createCriteria(aClass).addOrder(Order.asc(column)).list(); //tx.commit(); } catch (HibernateException e) { e.printStackTrace(); throw (e); } return results; } public static List get2OrderedList(Class aClass, String column, String column2) throws HibernateException { List results = null; try { //tx = session.beginTransaction(); results = session.createCriteria(aClass).addOrder(Order.asc(column)).addOrder(Order.asc(column2)).list(); //tx.commit(); } catch (HibernateException e) { e.printStackTrace(); throw (e); } return results; } /** * Requête de sélection HQL (Hibernate Query Language) * Renvoie la liste des tuples résultants */ public static List getList(String requete) throws HibernateException { List results = null; try { //tx = session.beginTransaction(); results = session.createQuery(requete).list(); //tx.commit(); } catch (HibernateException e) { e.printStackTrace(); throw (e); } return results; } /** * Requête de sélection d'un objet issu d'une table à partir de contraintes * SELECT * FROM table WHERE nomColonne=... * Renvoie le tuple recherché sous forme d'objet */ public static Object getObjectByColonne(Class c,String nomColonne,Object valeur) { Object object =null; try { //tx = session.beginTransaction(); object = session.createCriteria(c).add( Restrictions.eq(nomColonne, valeur)).uniqueResult(); //tx.commit(); } catch(HibernateException e) { e.printStackTrace(); return null; } return object; } /** * Requête de sélection d'un objet issu d'une table à partir de contraintes * SELECT * FROM table WHERE nomColonne1=... AND nomColonne2=... * Renvoie le tuple recherché sous forme d'objet */ public static Object getObjectBy2Colonne(Class c,String nomColonne,Object valeur,String nomColonne2,Object valeur2) { Object object =null; try { //tx = session.beginTransaction(); object = session.createCriteria(c) .add(Restrictions.eq(nomColonne, valeur)) .add(Restrictions.eq(nomColonne2, valeur2)) .uniqueResult(); //tx.commit(); } catch(HibernateException e) { e.printStackTrace(); return null; } return object; } /** * Requête de sélection d'un objet issu d'une table à partir de son identifiant * SELECT * FROM table WHERE table_id=... * Renvoie le tuple recherché sous forme d'objet */ public static Object getObjectById(Class c,Integer id) { Object object =null; try { //tx = session.beginTransaction(); object = session.get(c,id); //tx.commit(); } catch(HibernateException e) { e.printStackTrace(); return null; } return object; } /** * Insertion ou mise à jour d'un objet (<=> tuple) * INSERT INTO table VALUES (...) * UPDATE table SET ... */ public static void save(Object o) throws HibernateException { try { //tx = session.beginTransaction(); session.saveOrUpdate(o); //tx.commit(); } catch(HibernateException e) { e.printStackTrace(); throw(e); } } /** * Insertion ou mise à jour d'un objet (<=> tuple) * INSERT INTO table VALUES (...) */ public static void saveOnly(Object o) throws HibernateException { try { //tx = session.beginTransaction(); session.save(o); //tx.commit(); } catch(HibernateException e) { e.printStackTrace(); throw(e); } } /** * Suppression d'un objet (<=> tuple) * DELETE FROM table WHERE (...) */ public static void delete(Object o) throws HibernateException { try { //tx = session.beginTransaction(); session.delete(o); //tx.commit(); } catch(HibernateException e) { e.printStackTrace(); throw(e); } } /** * Insertion d'un objet à un identifiant donné * @throws Exception */ public static void save(Object o,Serializable id) throws HibernateException { try { //tx = session.beginTransaction(); id = session.save(o); //tx.commit(); } catch(HibernateException e) { e.printStackTrace(); throw(e); } } /** * Mise à jour d'un objet à un identifiant donné * @throws Exception */ public static void update(Object o) throws HibernateException { try { //tx = session.beginTransaction(); session.update(o); //tx.commit(); } catch(HibernateException e) { e.printStackTrace(); throw(e); } } /** * Nettoyage de la session * @throws Exception */ public static void clear() throws HibernateException { try { //tx = session.beginTransaction(); session.clear(); //tx.commit(); } catch(HibernateException e) { e.printStackTrace(); throw(e); } } /** * Sauvegarde de la base de données */ public static void commit() throws HibernateException { try { tx.commit(); } catch(HibernateException e) { e.printStackTrace(); throw(e); } } }