1
2
3
4
5 package org.directdemocracyportal.democracy.service.dao.hibernate;
6
7 import java.util.List;
8
9 import org.directdemocracyportal.democracy.model.world.Country;
10 import org.directdemocracyportal.democracy.model.world.Region;
11 import org.directdemocracyportal.democracy.service.dao.CountryDAO;
12 import org.hibernate.criterion.DetachedCriteria;
13 import org.hibernate.criterion.Restrictions;
14 import org.springframework.dao.DataAccessException;
15 import org.springframework.transaction.annotation.Propagation;
16 import org.springframework.transaction.annotation.Transactional;
17
18 /***
19 * The Class HibernateCountryDAO.
20 */
21 @Transactional(propagation = Propagation.MANDATORY)
22 public class HibernateCountryDAO extends GenericHibernateDAO<Country, Long>
23 implements CountryDAO
24 {
25
26 /***
27 * Instantiates a new hibernate country dao.
28 */
29 public HibernateCountryDAO() {
30 super(Country.class);
31 }
32
33
34
35
36
37
38 @Transactional(readOnly = true)
39 public Country findByName(String name) throws DataAccessException {
40 DetachedCriteria findByName = DetachedCriteria.forClass(Country.class);
41 findByName.add(Restrictions.eq("name", name));
42
43 @SuppressWarnings("unchecked")
44 List<Country> result = this.getHibernateTemplate().findByCriteria(
45 findByName);
46
47 return result.isEmpty() ? null : result.get(0);
48 }
49
50
51
52
53
54
55 @Transactional(readOnly = true)
56 public Region findRegionByName(String name) throws DataAccessException {
57 DetachedCriteria findByName = DetachedCriteria.forClass(Region.class);
58 findByName.add(Restrictions.eq("name", name));
59
60 @SuppressWarnings("unchecked")
61 List<Region> result = this.getHibernateTemplate().findByCriteria(
62 findByName);
63
64 return result.isEmpty() ? null : result.get(0);
65 }
66
67
68
69
70
71
72 @Override
73 public Region saveRegion(Region region) {
74 if (region.isNew()) {
75 this.getHibernateTemplate().save(region);
76 } else {
77 this.getHibernateTemplate().saveOrUpdate(region);
78 }
79 return region;
80 }
81 }