1 //----------------------------------------------------------------------
2 //
3 // PerfectJPattern: "Design patterns are good but components are better!"
4 // HibernateGenericDao.java Copyright (c) 2009 Giovanni Azua Garcia
5 // bravegag@hotmail.com
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License
9 // as published by the Free Software Foundation; either version 3
10 // of the License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, see <http://www.gnu.org/licenses/>.
19 //
20 //----------------------------------------------------------------------
21 package org.perfectjpattern.jee.integration.dao;
22
23 import java.io.*;
24 import java.util.*;
25
26 import javax.persistence.*;
27
28 import org.apache.commons.lang.*;
29 import org.hibernate.*;
30 import org.hibernate.criterion.*;
31 import org.perfectjpattern.jee.api.integration.dao.*;
32
33 /**
34 * Hibernate-based implementation of {@link IGenericReadOnlyDao}
35 *
36 * @param <Id> Identification type
37 * @param <Element> Element type
38 *
39 * @author <a href="mailto:bravegag@hotmail.com">Giovanni Azua</a>
40 * @version $Revision: 1.0 $ $Date: Nov 26, 2007 9:15:14 PM $
41 */
42 @SuppressWarnings("hiding")
43 public
44 class HibernateGenericDao<Id extends Serializable, Element>
45 extends JpaBaseDao<Id, Element>
46 implements IGenericDao<Id, Element>
47 {
48 //------------------------------------------------------------------------
49 // public
50 //------------------------------------------------------------------------
51 /**
52 * {@inheritDoc}
53 */
54 @SuppressWarnings("unchecked")
55 public List<Element>
56 findByExample(Element anElement, String ... anExcludeProperties)
57 throws DaoException, IllegalArgumentException
58 {
59 Validate.notNull(anElement, "'anElement' must not be null");
60 Validate.notNull(anExcludeProperties,
61 "'anExcludeProperties' must not be null");
62
63 List<Element> myElements = null;
64
65 try
66 {
67 Session mySession = getActualSession();
68
69 Criteria myCriteria = mySession.createCriteria(
70 getPersistentClass());
71 Example myExample = Example.create(anElement);
72 for (String myPropertyName : anExcludeProperties)
73 {
74 myExample.excludeProperty(myPropertyName);
75 }
76
77 myElements = myCriteria.add(myExample).list();
78
79 return myElements;
80 }
81 catch (HibernateException anException)
82 {
83 throw new DaoException(anException);
84 }
85 }
86
87 //------------------------------------------------------------------------
88 // protected
89 //------------------------------------------------------------------------
90 /**
91 * Constructs a {@link HibernateGenericDao} instance from the
92 * persistent class type, the {@link ISessionStrategy} that creates
93 * {@link Session} instances and the {@link ITransactionStrategy} that
94 * creates {@link Transaction} instances
95 *
96 * @param aPersistentClass The persistent Java Bean class
97 * @param aSessionStrategy Factory that creates Sessions
98 * @param aTransactionStrategy Factory that creates Transaction
99 */
100 protected
101 HibernateGenericDao(Class<Element> aPersistentClass,
102 ISessionStrategy aSessionStrategy,
103 ITransactionStrategy aTransactionStrategy)
104 throws IllegalArgumentException
105 {
106 super(aPersistentClass, aSessionStrategy, aTransactionStrategy);
107 }
108
109 //------------------------------------------------------------------------
110 /**
111 * Returns the Session
112 *
113 * @return the Session
114 */
115 protected Session
116 getActualSession()
117 throws DaoException
118 {
119 Object mySession = getSession().getDelegate();
120 while (mySession != null && mySession instanceof EntityManager)
121 {
122 mySession = ((EntityManager) mySession).getDelegate();
123 }
124
125 assert mySession instanceof Session :
126 "Actual Session must be a Hibernate Session";
127
128 return (Session) mySession;
129 }
130 }