source: ether_megapoli/trunk/service/implementation/com/medias/integration/utils/CommunicationBD.java @ 482

Last change on this file since 482 was 482, checked in by vmipsl, 12 years ago

BO insertion données _ insertion code medias package insertion

  • Property svn:executable set to *
File size: 7.8 KB
Line 
1/**
2 * Created on 17 Jan. 2007
3  * @author
4 */
5
6package integration.utils;
7import java.io.Serializable;
8import java.util.List;
9import org.hibernate.*;
10import org.hibernate.criterion.*;
11import integration.hibernate.HibernateSessionFactory;
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 Exception {
27        try {
28            session =  HibernateSessionFactory.currentSession();
29            tx = session.beginTransaction();
30        } catch (Exception e) {
31            e.printStackTrace();
32            throw (e);
33        }
34    }
35   
36    /**
37     * Fermeture de la session
38     */   
39    public static void close() throws Exception {
40        try {
41                tx.commit();
42            HibernateSessionFactory.closeSession();
43        } catch (Exception 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 Exception {
55        List<?> results = null;
56        try {
57                //tx = session.beginTransaction();
58            results = session.createCriteria(aClass).list();
59            //tx.commit();
60        } catch (Exception 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 Exception {
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 (Exception e) {
80            e.printStackTrace();
81            throw (e);
82        }
83        return results;
84    }
85
86    /**
87     * Requéte de sélection HQL (Hibernate Query Language)
88     * Renvoie la liste des tuples résultants
89     */
90    public static List<?> getList(String requete)
91    throws Exception {
92        List<?> results = null;
93        try {
94                //tx = session.beginTransaction();
95            results = session.createQuery(requete).list();
96            //tx.commit();
97        } catch (Exception e) {
98            e.printStackTrace();
99            throw (e);
100        }
101        return results;
102    }
103
104    public static void getSQL(String requete)
105    throws Exception {
106        try {
107                //tx = session.beginTransaction();
108            session.createSQLQuery(requete).uniqueResult();
109            //tx.commit();
110        } catch (Exception e) {
111            e.printStackTrace();
112            throw (e);
113        }
114    }
115   
116    /**
117     * Requéte de sélection d'un objet issu d'une table é partir de contraintes
118     * SELECT * FROM table WHERE nomColonne=...
119     * Renvoie le tuple recherché sous forme d'objet
120     */
121    public static Object getObjectByColonne(Class<?> c,String nomColonne,Object valeur) 
122    {
123        Object object =null;
124        try
125        {
126                //tx = session.beginTransaction();
127            object = session.createCriteria(c)
128                .add(Restrictions.eq(nomColonne, valeur))
129                .uniqueResult();
130            //tx.commit();
131        }
132        catch(Exception 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(Exception 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(Exception 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 Exception
193    {
194        try
195        {
196                //tx = session.beginTransaction();
197                session.saveOrUpdate(o);
198                //tx.commit();
199        }
200        catch(Exception 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 Exception
212    {
213        try
214        {
215                //tx = session.beginTransaction();
216                session.save(o);
217                //tx.commit();
218        }
219        catch(Exception 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 Exception
231    {
232        try
233        {
234                //tx = session.beginTransaction();
235            session.delete(o);
236            //tx.commit();
237        }
238        catch(Exception 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 Exception {
250        try
251        {
252                //tx = session.beginTransaction();
253            id = session.save(o);
254            //tx.commit();
255        }
256        catch(Exception 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 Exception {
268        try
269        {
270                //tx = session.beginTransaction();
271            session.update(o);
272            //tx.commit();
273        }
274        catch(Exception 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 Exception {
286        try
287        {
288                //tx = session.beginTransaction();
289            session.clear();
290            //tx.commit();
291        }
292        catch(Exception 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 Exception
303    {
304        try
305        {
306            tx.commit();
307        }
308        catch(Exception e) 
309        {
310            e.printStackTrace();
311            throw(e);
312        }
313    }
314   
315    /**
316     * Vérifie si un objet est déjé dans la session
317     */
318    public static void flush() {
319        session.flush();
320    }
321}
Note: See TracBrowser for help on using the repository browser.