1 package net
.aramzamzam
.commons
.hibernate
.dao
;
3 import java
.io
.Serializable
;
4 import java
.lang
.reflect
.ParameterizedType
;
7 import net
.aramzamzam
.commons
.hibernate
.utils
.AssociationExample
;
9 import org
.apache
.tapestry5
.hibernate
.HibernateSessionManager
;
10 import org
.hibernate
.Criteria
;
11 import org
.hibernate
.Session
;
12 import org
.hibernate
.criterion
.Criterion
;
13 import org
.hibernate
.criterion
.Example
;
15 /** DAO pattern from "Generic Data Access Objects": http://www.hibernate.org/328.html*/
16 public class GenericHibernateDAO
<T
, ID
extends Serializable
>
17 implements GenericDAO
<T
, ID
> {
19 private Class
<T
> persistentClass
;
20 private final HibernateSessionManager sessionManager
;
22 @SuppressWarnings("unchecked")
23 public GenericHibernateDAO(HibernateSessionManager sessionManager
){
24 this.persistentClass
= (Class
<T
>) ((ParameterizedType
) getClass()
25 .getGenericSuperclass()).getActualTypeArguments()[0];
26 this.sessionManager
= sessionManager
;
29 protected Session
getSession() {
30 return sessionManager
.getSession();
33 public Class
<T
> getPersistentClass() {
34 return persistentClass
;
37 public List
<T
> findAll() {
38 return findByCriteria();
42 * Finds list of entities by example
45 * @param exampleInstance
47 * @param includeAssociations
48 * include associations in search
49 * @param excludeProperty
50 * array of excluded properties
52 * list of founded entites (nulls and zero properties are expluded)
54 @SuppressWarnings("unchecked")
55 public <X
extends T
> List
<X
> findByExample(X exampleInstance
, boolean includeAssociations
, String
... excludeProperty
) {
56 Criteria crit
= getSession().createCriteria(exampleInstance
.getClass());
58 if (includeAssociations
)
60 AssociationExample e
= AssociationExample
.create(exampleInstance
);
61 e
.setIncludeAssociations(true);
62 for (String exclude
: excludeProperty
)
63 e
.excludeProperty(exclude
);
68 Example e
= Example
.create(exampleInstance
);
69 for (String exclude
: excludeProperty
)
70 e
.excludeProperty(exclude
);
77 public <X
extends T
> List
<X
> findByExample(X exampleInstance
) {
78 return findByExample(exampleInstance
,false, new String
[0]);
81 public T
makePersistent(T entity
) {
82 getSession().saveOrUpdate(entity
);
86 public void makeTransient(T entity
) {
87 getSession().delete(entity
);
99 * Use this inside subclasses as a convenience method.
101 @SuppressWarnings("unchecked")
102 protected List
<T
> findByCriteria(Criterion
... criterion
) {
103 Criteria crit
= getSession().createCriteria(getPersistentClass());
104 for (Criterion c
: criterion
) {
111 * returns null, if entity was not found
116 @SuppressWarnings("unchecked")
117 public T
findById(ID id
) {
118 return (T
) getSession().get(getPersistentClass(), id
);
122 * returns null, if entity was not found
129 @SuppressWarnings("unchecked")
130 public <X
extends T
> X
findById(ID id
, Class
<X
> entityClass
) {
131 return (X
) getSession().get(entityClass
, id
);