source: ether_megapoli/trunk/service/implementation/com/medias/integration_old/utils/CommunicationBD.java @ 486

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

BO insertion données _ ajout code medias
clean

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