1 package org.smartcomps.twister.common.persistence;
2
3 import net.sf.hibernate.HibernateException;
4 import net.sf.hibernate.Session;
5 import org.apache.commons.logging.Log;
6 import org.apache.commons.logging.LogFactory;
7
8 import java.util.List;
9
10 public class PersistentDataAccess {
11
12 private static Log log = LogFactory.getLog(PersistentDataAccess.class);
13
14 public static Object create(Object po) throws DBSessionException {
15 Session session = DBSessionManager.getActiveSession();
16
17 try {
18 session.save(po);
19 } catch (HibernateException e) {
20 log.error("Problem when creating a persistent object.", e);
21 throw new DBSessionException(e);
22 }
23
24 return po;
25 }
26
27 public static Object update(Object po) throws DBSessionException {
28 Session session = DBSessionManager.getActiveSession();
29
30 try {
31 session.update(po);
32 } catch (HibernateException e) {
33 log.error("Problem when updating a persistent object.", e);
34 throw new DBSessionException(e);
35 }
36
37 return po;
38 }
39
40 public static Object remove(Object po) throws DBSessionException {
41 Session session = DBSessionManager.getActiveSession();
42
43 try {
44 session.delete(po);
45 } catch (HibernateException e) {
46 log.error("Problem when updating a persistent object.", e);
47 throw new DBSessionException(e);
48 }
49
50 return po;
51 }
52
53 public static Object findById(Long id, Class classToLoad) throws DBSessionException, FinderException {
54 Session session = DBSessionManager.getActiveSession();
55 Object po = null;
56 try {
57 po = session.load(classToLoad, id);
58 } catch (HibernateException e) {
59 log.error("Problem when updating a persistent object.", e);
60 throw new DBSessionException(e);
61 }
62 if (po == null) {
63 throw new FinderException("Couldn't find a " + classToLoad + " with id : " + id);
64 }
65
66 return po;
67 }
68
69 public static List findAll(Class classToLoad) throws DBSessionException {
70 Session session = DBSessionManager.getActiveSession();
71
72 List results = null;
73 try {
74 results = session.find("FROM " + classToLoad.getName());
75 } catch (HibernateException e) {
76 throw new DBSessionException(e);
77 }
78 return results;
79 }
80
81 }
This page was automatically generated by Maven