Project

General

Profile

« Previous | Next » 

Revision 34795

method count() in daos

View differences:

modules/dnet-information-service/trunk/src/main/java/eu/dnetlib/enabling/is/dao/DnetSimpleDao.java
8 8

  
9 9
public interface DnetSimpleDao<T extends HibDnetObject> {
10 10

  
11
	public List<T> list() throws InformationServiceException;
11
	List<T> list() throws InformationServiceException;
12 12

  
13 13
	List<T> list(String col, Object value) throws InformationServiceException;
14 14

  
15
	public T find(Serializable id) throws InformationServiceException;
15
	T find(Serializable id) throws InformationServiceException;
16 16

  
17
	public void save(T s) throws InformationServiceException;
17
	void save(T s) throws InformationServiceException;
18 18

  
19
	public void delete(String id) throws InformationServiceException;
19
	void delete(String id) throws InformationServiceException;
20 20

  
21
	long count() throws InformationServiceException;
21 22
}
modules/dnet-information-service/trunk/src/main/java/eu/dnetlib/enabling/is/dao/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