Revision 34795
Added by Michele Artini over 9 years ago
DnetSimpleDaoImpl.java | ||
---|---|---|
31 | 31 |
|
32 | 32 |
@Override |
33 | 33 |
public void save(final T hib) throws InformationServiceException { |
34 |
final Session session = sessionFactory.openSession(); |
|
34 | 35 |
try { |
35 |
final Session session = sessionFactory.openSession(); |
|
36 | 36 |
final Transaction tx = session.beginTransaction(); |
37 | 37 |
hib.save(session); |
38 | 38 |
tx.commit(); |
39 |
session.close(); |
|
40 | 39 |
} catch (Throwable e) { |
41 | 40 |
log.error("Error saving resource, class: " + clazz, e); |
42 | 41 |
throw new InformationServiceException("Error saving resource, class: " + clazz, e); |
42 |
} finally { |
|
43 |
session.close(); |
|
43 | 44 |
} |
44 | 45 |
} |
45 | 46 |
|
47 |
@SuppressWarnings("unchecked") |
|
46 | 48 |
@Override |
47 | 49 |
public List<T> list() throws InformationServiceException { |
50 |
final Session session = sessionFactory.openSession(); |
|
48 | 51 |
try { |
49 |
final Session session = sessionFactory.openSession(); |
|
50 |
@SuppressWarnings("unchecked") |
|
51 |
final List<T> list = session.createQuery("from " + table).list(); |
|
52 |
session.close(); |
|
53 |
return list; |
|
52 |
return session.createQuery("from " + table).list(); |
|
54 | 53 |
} catch (Throwable e) { |
55 | 54 |
log.error("Error listing resources, class: " + clazz, e); |
56 | 55 |
throw new InformationServiceException("Error listing resources, class: " + clazz, e); |
56 |
} finally { |
|
57 |
session.close(); |
|
57 | 58 |
} |
59 |
|
|
58 | 60 |
} |
59 | 61 |
|
62 |
@SuppressWarnings("unchecked") |
|
60 | 63 |
@Override |
61 | 64 |
public List<T> list(final String col, final Object value) throws InformationServiceException { |
65 |
final Session session = sessionFactory.openSession(); |
|
62 | 66 |
try { |
63 |
final Session session = sessionFactory.openSession(); |
|
64 | 67 |
final Query query = session.createQuery("from " + table + " where " + col + " = :value"); |
65 | 68 |
query.setParameter("value", value); |
66 |
@SuppressWarnings("unchecked") |
|
67 |
final List<T> list = query.list(); |
|
68 |
session.close(); |
|
69 |
return list; |
|
69 |
return query.list(); |
|
70 | 70 |
} catch (Throwable e) { |
71 | 71 |
log.error("Error listing resources, class: " + clazz, e); |
72 | 72 |
throw new InformationServiceException("Error listing resources, class: " + clazz, e); |
73 |
} finally { |
|
74 |
session.close(); |
|
73 | 75 |
} |
74 | 76 |
} |
75 | 77 |
|
76 | 78 |
@Override |
77 | 79 |
public T find(final Serializable id) throws InformationServiceException { |
80 |
final Session session = sessionFactory.openSession(); |
|
78 | 81 |
try { |
79 |
final Session session = sessionFactory.openSession(); |
|
80 |
final T hib = clazz.cast(session.get(clazz, id)); |
|
81 |
session.close(); |
|
82 |
return hib; |
|
82 |
return clazz.cast(session.get(clazz, id)); |
|
83 | 83 |
} catch (Throwable e) { |
84 | 84 |
log.error("Error obtaining resource, class: " + clazz + ", id: " + id, e); |
85 | 85 |
throw new InformationServiceException("Error obtaining resource, class: " + clazz + ", id: " + id, e); |
86 |
} finally { |
|
87 |
session.close(); |
|
86 | 88 |
} |
87 | 89 |
} |
88 | 90 |
|
89 | 91 |
@Override |
90 | 92 |
public void delete(final String id) throws InformationServiceException { |
93 |
final Session session = sessionFactory.openSession(); |
|
91 | 94 |
try { |
92 |
final Session session = sessionFactory.openSession(); |
|
93 | 95 |
final Transaction tx = session.beginTransaction(); |
94 | 96 |
final T hib = clazz.cast(session.get(clazz, id)); |
95 | 97 |
hib.delete(session); |
96 | 98 |
tx.commit(); |
97 |
session.close(); |
|
98 | 99 |
} catch (Throwable e) { |
99 | 100 |
log.error("Error deleting resource, class: " + clazz + ", id: " + id, e); |
100 | 101 |
throw new InformationServiceException("Error deleting resource, class: " + clazz + ", id: " + id, e); |
102 |
} finally { |
|
103 |
session.close(); |
|
101 | 104 |
} |
102 | 105 |
} |
103 | 106 |
|
107 |
@Override |
|
108 |
public long count() throws InformationServiceException { |
|
109 |
final Session session = sessionFactory.openSession(); |
|
110 |
try { |
|
111 |
return ((Number) session.createQuery("select count(*) from " + table).uniqueResult()).longValue(); |
|
112 |
} catch (Throwable e) { |
|
113 |
log.error("Error listing resources, class: " + clazz, e); |
|
114 |
throw new InformationServiceException("Error listing resources, class: " + clazz, e); |
|
115 |
} finally { |
|
116 |
session.close(); |
|
117 |
} |
|
118 |
} |
|
119 |
|
|
104 | 120 |
public SessionFactory getSessionFactory() { |
105 | 121 |
return sessionFactory; |
106 | 122 |
} |
... | ... | |
116 | 132 |
public void setClazz(final Class<T> clazz) { |
117 | 133 |
this.clazz = clazz; |
118 | 134 |
} |
135 |
|
|
119 | 136 |
} |
Also available in: Unified diff
method count() in daos