001 // Copyright 2007-2008 Thiago H. de Paula Figueiredo
002 //
003 // Licensed under the Apache License, Version 2.0 (the "License");
004 // you may not use this file except in compliance with the License.
005 // You may obtain a copy of the License at
006 //
007 // http://www.apache.org/licenses/LICENSE-2.0
008 //
009 // Unless required by applicable law or agreed to in writing, software
010 // distributed under the License is distributed on an "AS IS" BASIS,
011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 // See the License for the specific language governing permissions and
013 // limitations under the License.
014
015 package br.com.arsmachina.dao.hibernate;
016
017 import java.io.Serializable;
018
019 import org.hibernate.EntityMode;
020 import org.hibernate.Query;
021 import org.hibernate.SessionFactory;
022 import org.hibernate.classic.Session;
023 import org.hibernate.metadata.ClassMetadata;
024
025 import br.com.arsmachina.dao.WriteableDAO;
026
027 /**
028 * {@link WriteableDAO} implementation using Hibernate. All methods use {@link #getSession()} to get
029 * a {@link Session}.
030 *
031 * @author Thiago H. de Paula Figueiredo
032 * @param <T> the entity class related to this DAO.
033 * @param <K> the type of the field that represents the entity class' primary key.
034 */
035 public abstract class WriteableDAOImpl<T, K extends Serializable> extends BaseHibernateDAO<T, K>
036 implements WriteableDAO<T, K> {
037
038 final private String deleteHQL;
039
040 /**
041 * Constructor that takes a {@link Class} and a {@link SessionFactory}.
042 *
043 * @param clasz a {@link Class}.
044 * @param sessionFactory a {@link SessionFactory}. It cannot be null.
045 */
046 public WriteableDAOImpl(SessionFactory sessionFactory) {
047 this(null, sessionFactory);
048 }
049
050 /**
051 * Constructor that takes a {@link Class} and a {@link SessionFactory}.
052 *
053 * @param clasz a {@link Class}.
054 * @param sessionFactory a {@link SessionFactory}. It cannot be null.
055 */
056 @SuppressWarnings("unchecked")
057 public WriteableDAOImpl(Class<T> clasz, SessionFactory sessionFactory) {
058
059 super(clasz, sessionFactory);
060 deleteHQL = createDeleteHQL();
061
062 }
063
064 /**
065 * Creates an HQL query used to delete an object given its primary key value.
066 *
067 * @return a {@link String}.
068 */
069 String createDeleteHQL() {
070
071 return "delete from " + getEntityClass().getName() + " where "
072 + getPrimaryKeyPropertyName() + " = :id";
073
074 }
075
076 public void delete(K id) {
077
078 Query query = getSession().createQuery(deleteHQL);
079 query.setParameter("id", id);
080 query.executeUpdate();
081
082 }
083
084 public void delete(T object) {
085 getSession().delete(object);
086 }
087
088 public void evict(T object) {
089 getSession().evict(object);
090 }
091
092 public void save(T object) {
093 getSession().save(object);
094 }
095
096 public T update(T object) {
097
098 if (isPersistent(object) == false) {
099 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 }