View Javadoc

1   // Copyright 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  import java.lang.reflect.ParameterizedType;
19  import java.lang.reflect.Type;
20  
21  import org.hibernate.SessionFactory;
22  import org.hibernate.classic.Session;
23  import org.hibernate.metadata.ClassMetadata;
24  
25  /**
26   * Superclass of both {@link ReadableDAOImpl} and {@link WriteableDAOImpl}.
27   * 
28   * @author Thiago H. de Paula Figueiredo
29   * @param <T> the entity class related to this DAO.
30   * @param <K> the type of the field that represents the entity class' primary key.
31   */
32  public class BaseHibernateDAO<T, K extends Serializable> {
33  
34  	private final SessionFactory sessionFactory;
35  
36  	private final Class<T> entityClass;
37  
38  	private final ClassMetadata classMetadata;
39  
40  	private final String primaryKeyPropertyName;
41  
42  	/**
43  	 * Constructor that takes a {@link Class} and a {@link SessionFactory}.
44  	 * 
45  	 * @param clasz a {@link Class}.
46  	 * @param sessionFactory a {@link SessionFactory}. It cannot be null.
47  	 */
48  	@SuppressWarnings("unchecked")
49  	public BaseHibernateDAO(SessionFactory sessionFactory) {
50  		this(null, sessionFactory);
51  	}
52  
53  	/**
54  	 * Constructor that takes a {@link Class} and a {@link SessionFactory}.
55  	 * 
56  	 * @param clasz a {@link Class}.
57  	 * @param sessionFactory a {@link SessionFactory}. It cannot be null.
58  	 */
59  	@SuppressWarnings("unchecked")
60  	public BaseHibernateDAO(Class<T> clasz, SessionFactory sessionFactory) {
61  
62  		if (sessionFactory == null) {
63  			throw new IllegalArgumentException("Parameter sessionFactory cannot be null");
64  		}
65  
66  		this.sessionFactory = sessionFactory;
67  
68  		entityClass = clasz != null ? clasz : extractEntityClassFromHierarchy();
69  		classMetadata = sessionFactory.getClassMetadata(getEntityClass());
70  
71  		if (getClassMetadata() == null) {
72  			throw new RuntimeException("Class " + getEntityClass().getName() + " is not mapped");
73  		}
74  
75  		primaryKeyPropertyName = getClassMetadata().getIdentifierPropertyName();
76  
77  		assert getEntityClass() != null;
78  		assert getClassMetadata() != null;
79  		assert getPrimaryKeyPropertyName() != null;
80  
81  	}
82  
83  	/**
84  	 * @return
85  	 */
86  	@SuppressWarnings("unchecked")
87  	private Class<T> extractEntityClassFromHierarchy() {
88  
89  		final Type genericSuperclass = getClass().getGenericSuperclass();
90  		final ParameterizedType parameterizedType = ((ParameterizedType) genericSuperclass);
91  		return (Class<T>) parameterizedType.getActualTypeArguments()[0];
92  
93  	}
94  
95  	/**
96  	 * Returns the entity class handled by this DAO.
97  	 * 
98  	 * @return a {@link Class<T>}.
99  	 */
100 	protected final Class<T> getEntityClass() {
101 		return entityClass;
102 	}
103 
104 	/**
105 	 * Returns a {@link Session}. This implementation returns
106 	 * {@link SessionFactory#getCurrentSession()} and can be overriden if needed.
107 	 * 
108 	 * @return a {@link Session}.
109 	 */
110 	protected Session getSession() {
111 		return getSessionFactory().getCurrentSession();
112 	}
113 
114 	/**
115 	 * Returns this DAO's {@link SessionFactory}.
116 	 * 
117 	 * @return a {@link SessionFactory}.
118 	 */
119 	protected final SessionFactory getSessionFactory() {
120 		return sessionFactory;
121 	}
122 
123 	/**
124 	 * Returns the {@link ClassMetadata} for the corresponding entity class.
125 	 * 
126 	 * @return a {@link ClassMetadata}.
127 	 */
128 	protected final ClassMetadata getClassMetadata() {
129 		return classMetadata;
130 	}
131 
132 	/**
133 	 * Returns the name of the id property.
134 	 * 
135 	 * @return a {@link String}.
136 	 */
137 	protected String getPrimaryKeyPropertyName() {
138 		return primaryKeyPropertyName;
139 	}
140 
141 }