1 // Copyright 2007-2008 Thiago H. de Paula Figueiredo
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package br.com.arsmachina.dao.hibernate;
16
17 import java.io.Serializable;
18
19 import org.hibernate.EntityMode;
20 import org.hibernate.Query;
21 import org.hibernate.SessionFactory;
22 import org.hibernate.classic.Session;
23 import org.hibernate.metadata.ClassMetadata;
24
25 import br.com.arsmachina.dao.WriteableDAO;
26
27 /**
28 * {@link WriteableDAO} implementation using Hibernate. All methods use {@link #getSession()} to get
29 * a {@link Session}.
30 *
31 * @author Thiago H. de Paula Figueiredo
32 * @param <T> the entity class related to this DAO.
33 * @param <K> the type of the field that represents the entity class' primary key.
34 */
35 public abstract class WriteableDAOImpl<T, K extends Serializable> extends BaseHibernateDAO<T, K>
36 implements WriteableDAO<T, K> {
37
38 final private String deleteHQL;
39
40 /**
41 * Constructor that takes a {@link Class} and a {@link SessionFactory}.
42 *
43 * @param clasz a {@link Class}.
44 * @param sessionFactory a {@link SessionFactory}. It cannot be null.
45 */
46 public WriteableDAOImpl(SessionFactory sessionFactory) {
47 this(null, sessionFactory);
48 }
49
50 /**
51 * Constructor that takes a {@link Class} and a {@link SessionFactory}.
52 *
53 * @param clasz a {@link Class}.
54 * @param sessionFactory a {@link SessionFactory}. It cannot be null.
55 */
56 @SuppressWarnings("unchecked")
57 public WriteableDAOImpl(Class<T> clasz, SessionFactory sessionFactory) {
58
59 super(clasz, sessionFactory);
60 deleteHQL = createDeleteHQL();
61
62 }
63
64 /**
65 * Creates an HQL query used to delete an object given its primary key value.
66 *
67 * @return a {@link String}.
68 */
69 String createDeleteHQL() {
70
71 return "delete from " + getEntityClass().getName() + " where "
72 + getPrimaryKeyPropertyName() + " = :id";
73
74 }
75
76 public void delete(K id) {
77
78 Query query = getSession().createQuery(deleteHQL);
79 query.setParameter("id", id);
80 query.executeUpdate();
81
82 }
83
84 public void delete(T object) {
85 getSession().delete(object);
86 }
87
88 public void evict(T object) {
89 getSession().evict(object);
90 }
91
92 public void save(T object) {
93 getSession().save(object);
94 }
95
96 public T update(T object) {
97
98 if (isPersistent(object) == false) {
99 throw new IllegalArgumentException("Object not persistent");
100 }
101
102 getSession().update(object);
103 return object;
104
105 }
106
107 /**
108 * Returns <code>true</code> if the primary key field (identifier) of the given object is not
109 * null. Its value is obtained via {@link ClassMetadata#getIdentifier(Object, EntityMode)}.
110 *
111 * @see br.com.arsmachina.dao.WriteableDAO#isPersistent(java.lang.Object)
112 * @throws IllegalArgumentException if <code>object</code> is null.
113 */
114 public boolean isPersistent(T object) {
115
116 if (object == null) {
117 throw new IllegalArgumentException("Parameter object cannot be null");
118 }
119
120 return getClassMetadata().getIdentifier(object, EntityMode.POJO) != null;
121
122 }
123
124 }