1 package com.germinus.merlin.dao;
2
3 import java.io.Serializable;
4 import java.util.List;
5
6 import org.apache.commons.logging.Log;
7 import org.apache.commons.logging.LogFactory;
8 import org.springframework.orm.ObjectRetrievalFailureException;
9 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 public abstract class GenericDaoHibernate<T, PK extends Serializable>
27 extends HibernateDaoSupport implements IGenericDao<T, PK> {
28
29 protected final Log log = LogFactory.getLog(getClass());
30 private Class<T> persistentClass;
31
32 public GenericDaoHibernate(Class<T> persistentClass) {
33 this.persistentClass = persistentClass;
34 }
35
36 @SuppressWarnings("unchecked")
37 public List<T> getAll() {
38 return super.getHibernateTemplate().loadAll(this.persistentClass);
39 }
40
41 @SuppressWarnings("unchecked")
42 public T get(PK id) {
43 T entity = (T) super.getHibernateTemplate().get(this.persistentClass, id);
44
45 if (entity == null) {
46 log.warn("Uh oh, '" + this.persistentClass + "' object with id '" + id + "' not found...");
47 throw new ObjectRetrievalFailureException(this.persistentClass, id);
48 }
49
50 return entity;
51 }
52
53 @SuppressWarnings("unchecked")
54 public boolean exists(PK id) {
55 T entity = (T) super.getHibernateTemplate().get(this.persistentClass, id);
56 if (entity == null) {
57 return false;
58 } else {
59 return true;
60 }
61 }
62
63 @SuppressWarnings("unchecked")
64 public T save(T object) {
65 return (T) super.getHibernateTemplate().merge(object);
66 }
67
68 public void remove(PK id) {
69 super.getHibernateTemplate().delete(this.get(id));
70 }
71
72 }