View Javadoc

1   /*
2    * Copyright (C) 2007 Alf Mikula
3    * 
4    * This file is part of PromoteGo.
5    *
6    * PromoteGo is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * PromoteGo is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with PromoteGo.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package org.promotego.dao.hibernate;
20  
21  import java.util.List;
22  
23  import org.promotego.dao.interfaces.Dao;
24  import org.springframework.orm.hibernate3.HibernateTemplate;
25  
26  public abstract class HibernateDaoSupport<T>
27          extends org.springframework.orm.hibernate3.support.HibernateDaoSupport
28          implements Dao<T>
29  {
30      public void create(T tee)
31      {
32          HibernateTemplate ht = getHibernateTemplate();
33          
34          ht.save(tee);
35      }
36  
37      public void create(List<T> listTees)
38      {
39          HibernateTemplate ht = getHibernateTemplate();
40          
41          for (T thisTee : listTees)
42          {
43              ht.save(thisTee);
44          }
45      }
46  
47      public List<T> findAll()
48      {
49          HibernateTemplate ht = getHibernateTemplate();
50          
51          @SuppressWarnings("unchecked")
52              List<T> retval = (List<T>)ht.find(getFindAllQuery());
53          
54          return retval;
55      }
56  
57      public T getById(Long id)
58      {
59          HibernateTemplate ht = getHibernateTemplate();
60      
61          @SuppressWarnings("unchecked")
62              List<T> foundTees = ht.find(getIdQuery(), id);
63          
64          assert foundTees.size() < 2 : "Id column is not unique";
65          
66          if (foundTees.size() == 0)
67          {
68              return null;
69          }
70          else
71          {
72              return foundTees.get(0);
73          }
74      }
75      
76      public void delete(T theTee)
77      {
78          HibernateTemplate ht = getHibernateTemplate();
79          
80          ht.delete(theTee);
81      }
82  
83      public void deleteAll()
84      {
85          HibernateTemplate ht = getHibernateTemplate();
86          
87          ht.deleteAll(findAll());
88      }
89      
90      public void reattach(T theTee)
91      {
92          getHibernateTemplate().refresh(theTee);
93      }
94      
95      public void update(T theTee)
96      {
97      	getHibernateTemplate().update(theTee);
98      }
99      
100     public void saveOrUpdate(T theTee)
101     {
102     	getHibernateTemplate().saveOrUpdate(theTee);
103     }
104     
105 	public int getCount()
106 	{
107 	    @SuppressWarnings("unchecked")
108     	List result = getHibernateTemplate().find(getCountQuery());
109     	return ((Long)result.get(0)).intValue();
110 	}
111     
112     protected abstract String getFindAllQuery();
113     protected abstract String getIdQuery();
114     protected abstract String getCountQuery();
115 }