source: ether_megapoli/trunk/service/implementation/com/medias/utils/hibernate/CommunicationBD.java @ 168

Last change on this file since 168 was 168, checked in by vmipsl, 13 years ago

Import medias files and cleanup

  • Property svn:executable set to *
File size: 8.0 KB
Line 
1/**
2 * Created on 17 Jan. 2007
3  * @author
4 */
5
6package com.medias.utils.hibernate;
7import java.io.Serializable;
8import java.util.List;
9import org.hibernate.*;
10import org.hibernate.criterion.Order;
11import org.hibernate.criterion.Restrictions;
12
13/**
14 *
15 * AccÚs et échanges avec la base de données
16 */
17
18public class CommunicationBD {
19
20    private static Transaction tx = null;
21    private static Session session = null;
22   
23    /**
24     * Ouverture de la session
25     */
26    public static void open() throws HibernateException {
27        try {
28            session =  SessionManager.currentSession();
29            tx = session.beginTransaction();
30        } catch (HibernateException e) {
31            e.printStackTrace();
32            throw (e);
33        }
34    }
35   
36    /**
37     * Fermeture de la session
38     */   
39    public static void close() throws HibernateException {
40        try {
41                tx.commit();
42                SessionManager.closeSession();
43        } catch (HibernateException e) {
44            e.printStackTrace();
45            throw (e);
46        }
47    }
48 
49    /**
50     * Requête de sélection sur une table
51     * SELECT * FROM table;
52     * Renvoie la liste des tuples de la table
53     */
54    public static List<?> getAllList(Class<?> aClass) throws HibernateException {
55        List<?> results = null;
56        try {
57                //tx = session.beginTransaction();
58            results = session.createCriteria(aClass).list();
59            //tx.commit();
60        } catch (HibernateException e) {
61            e.printStackTrace();
62            throw (e);
63        }
64        return results;
65       
66    }
67    /**
68     * Requête de sélection ordonnée sur une table
69     * SELECT * FROM table ORDER BY colonne;
70     * Renvoie la liste des tuples de la table
71     */
72    public static List<?> getOrderedList(Class<?> aClass, String column)
73    throws HibernateException {
74        List<?> results = null;
75        try {
76                //tx = session.beginTransaction();
77            results = session.createCriteria(aClass).addOrder(Order.asc(column)).list();
78            //tx.commit();
79        } catch (HibernateException e) {
80            e.printStackTrace();
81            throw (e);
82        }
83        return results;
84    }
85    public static List<?> get2OrderedList(Class<?> aClass, String column, String column2)
86    throws HibernateException {
87        List<?> results = null;
88        try {
89                //tx = session.beginTransaction();
90            results = session.createCriteria(aClass).addOrder(Order.asc(column)).addOrder(Order.asc(column2)).list();
91            //tx.commit();
92        } catch (HibernateException e) {
93            e.printStackTrace();
94            throw (e);
95        }
96        return results;
97    }
98   
99    /**
100     * Requête de sélection HQL (Hibernate Query Language)
101     * Renvoie la liste des tuples résultants
102     */
103    public static List<?> getList(String requete)
104    throws HibernateException {
105        List<?> results = null;
106        try {
107                //tx = session.beginTransaction();
108            results = session.createQuery(requete).list();
109            //tx.commit();
110        } catch (HibernateException e) {
111            e.printStackTrace();
112            throw (e);
113        }
114        return results;
115    }
116
117    /**
118     * Requête de sélection d'un objet issu d'une table à partir de contraintes
119     * SELECT * FROM table WHERE nomColonne=...
120     * Renvoie le tuple recherché sous forme d'objet
121     */
122    public static Object getObjectByColonne(Class<?> c,String nomColonne,Object valeur) 
123    {
124        Object object =null;
125        try
126        {
127                //tx = session.beginTransaction();
128            object = session.createCriteria(c).add(
129                        Restrictions.eq(nomColonne, valeur)).uniqueResult();
130            //tx.commit();
131        }
132        catch(HibernateException e) 
133        {
134            e.printStackTrace();
135            return null;
136        }
137        return object;
138    }
139
140    /**
141     * Requête de sélection d'un objet issu d'une table à partir de contraintes
142     * SELECT * FROM table WHERE nomColonne1=... AND nomColonne2=...
143     * Renvoie le tuple recherché sous forme d'objet
144     */
145    public static Object getObjectBy2Colonne(Class<?> c,String nomColonne,Object valeur,String nomColonne2,Object valeur2) 
146    {
147        Object object =null;
148        try
149        {
150                //tx = session.beginTransaction();
151            object  = session.createCriteria(c)
152                .add(Restrictions.eq(nomColonne, valeur))
153                .add(Restrictions.eq(nomColonne2, valeur2))
154                .uniqueResult();
155            //tx.commit();
156        }
157        catch(HibernateException e) 
158        {
159            e.printStackTrace();
160            return null;
161        }
162        return object;
163    }
164   
165    /**
166     * Requête de sélection d'un objet issu d'une table à partir de son identifiant
167     * SELECT * FROM table WHERE table_id=...
168     * Renvoie le tuple recherché sous forme d'objet
169     */
170    public static Object getObjectById(Class<?> c,Integer id) 
171    {
172        Object object =null;
173        try
174        {
175                //tx = session.beginTransaction();
176            object  = session.get(c,id);
177            //tx.commit();
178        }
179        catch(HibernateException e) 
180        {
181            e.printStackTrace();
182            return null;
183        }
184        return object;
185    }
186   
187    /**
188     * Insertion ou mise à jour d'un objet (<=> tuple)
189     * INSERT INTO table VALUES (...)
190     * UPDATE table SET ...
191     */
192    public static void save(Object o) throws HibernateException
193    {
194        try
195        {
196                //tx = session.beginTransaction();
197                session.saveOrUpdate(o);
198                //tx.commit();
199        }
200        catch(HibernateException e) 
201        {
202            e.printStackTrace();
203            throw(e);
204        }
205    }
206   
207    /**
208     * Insertion ou mise à jour d'un objet (<=> tuple)
209     * INSERT INTO table VALUES (...)
210     */
211    public static void saveOnly(Object o) throws HibernateException
212    {
213        try
214        {
215                //tx = session.beginTransaction();
216                session.save(o);
217                //tx.commit();
218        }
219        catch(HibernateException e) 
220        {
221            e.printStackTrace();
222            throw(e);
223        }
224    }
225   
226    /**
227     * Suppression d'un objet (<=> tuple)
228     * DELETE FROM table WHERE (...)
229     */
230    public static void delete(Object o) throws HibernateException
231    {
232        try
233        {
234                //tx = session.beginTransaction();
235            session.delete(o);
236            //tx.commit();
237        }
238        catch(HibernateException e) 
239        {
240            e.printStackTrace();
241            throw(e);
242        }
243    }
244
245    /**
246     * Insertion d'un objet à un identifiant donné
247     * @throws Exception
248     */
249    public static void save(Object o,Serializable id) throws HibernateException {
250        try
251        {
252                //tx = session.beginTransaction();
253            id = session.save(o);
254            //tx.commit();
255        }
256        catch(HibernateException e) 
257        {
258            e.printStackTrace();
259            throw(e);
260        }
261    }
262   
263    /**
264     * Mise à jour d'un objet à un identifiant donné
265     * @throws Exception
266     */
267    public static void update(Object o) throws HibernateException {
268        try
269        {
270                //tx = session.beginTransaction();
271            session.update(o);
272            //tx.commit();
273        }
274        catch(HibernateException e) 
275        {
276            e.printStackTrace();
277            throw(e);
278        }
279    }
280
281    /**
282     * Nettoyage de la session
283     * @throws Exception
284     */
285    public static void clear() throws HibernateException {
286        try
287        {
288                //tx = session.beginTransaction();
289            session.clear();
290            //tx.commit();
291        }
292        catch(HibernateException e) 
293        {
294            e.printStackTrace();
295            throw(e);
296        }
297    }
298   
299    /**
300     * Sauvegarde de la base de données
301     */
302    public static void commit() throws HibernateException
303    {
304        try
305        {
306            tx.commit();
307        }
308        catch(HibernateException e) 
309        {
310            e.printStackTrace();
311            throw(e);
312        }
313    }
314}
Note: See TracBrowser for help on using the repository browser.