1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.promotego.dao.hibernate;
20
21 import java.util.List;
22
23 import org.promotego.beans.Address;
24 import org.promotego.dao.interfaces.AddressDao;
25 import org.springframework.orm.hibernate3.HibernateTemplate;
26 import org.springframework.transaction.annotation.Transactional;
27
28 public class HibernateAddressDao extends HibernateDaoSupport<Address> implements AddressDao
29 {
30 public String getFindAllQuery() { return "From Address"; }
31 public String getIdQuery() { return "From Address where id=?"; }
32 public String getCountQuery() { return "Select count(*) from Address"; }
33
34 @Transactional
35 public Address getByName(String name, Long userId)
36 {
37 HibernateTemplate ht = getHibernateTemplate();
38
39 @SuppressWarnings("unchecked")
40 List<Address> results = (List<Address>)ht.find("From Address where name=? and userId=?", new Object[] { name, userId });
41
42
43 assert results.size() < 2 : "Unique address name per user constraint violated";
44
45 if (results.size() == 0)
46 {
47 return null;
48 }
49 else
50 {
51 return results.get(0);
52 }
53 }
54 }