View Javadoc

1   //----------------------------------------------------------------------
2   // 
3   // PerfectJPattern: "Design patterns are good but components are better!" 
4   // HibernateSessionAdapter.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.lang.reflect.*;
25  
26  import org.hibernate.*;
27  import org.perfectjpattern.core.api.structural.adapter.*;
28  import org.perfectjpattern.core.structural.adapter.*;
29  import org.perfectjpattern.jee.api.integration.dao.*;
30  
31  /**
32   * Adapts Hibernate's {@link Session} to the JPA implementation-free 
33   * PerfectJPattern's {@link ISession} definition 
34   * 
35   * @author <a href="mailto:bravegag@hotmail.com">Giovanni Azua</a>
36   * @version $Revision: 1.0 $Date: Feb 10, 2009 8:26:06 PM $
37   */ 
38  public final 
39  class HibernateSessionAdapter
40  extends Adapter<ISession, Session>
41  {
42      //------------------------------------------------------------------------
43      // public
44      //------------------------------------------------------------------------
45      /**
46       * Constructs a {@link HibernateSessionAdapter} from the Adaptee 
47       * {@link Session} instance
48       * 
49       * @param anAdaptee Adaptee Hibernate Transaction
50       * @throws IllegalArgumentException
51       */
52      public 
53      HibernateSessionAdapter(Session anAdaptee)
54      throws IllegalArgumentException
55      {
56          super(ISession.class, anAdaptee);
57      }
58  
59      //------------------------------------------------------------------------
60      // protected
61      //------------------------------------------------------------------------
62      /** 
63       * {@inheritDoc}
64       */
65      @Override
66      protected Object 
67      invokeUnderlying(Method aMethod, Object[] anArguments)
68      throws Throwable
69      {
70          try
71          {
72              return super.invokeUnderlying(aMethod, anArguments);
73          }
74          // CHECKSTYLE:OFF
75          catch (Throwable anException)
76          // CHECKSTYLE:ON
77          {
78              // make sure that Exceptions are properly chained
79              throw new DaoException(anException);
80          }
81      }
82      
83      //------------------------------------------------------------------------
84      public ITransaction
85      getTransaction()
86      {
87          Transaction myAdaptee = getUnderlying().getTransaction();
88  
89          if (theTransactionAdapter == null || theTransactionAdapter.
90              getAdaptee() != myAdaptee)
91          {
92              theTransactionAdapter = new HibernateTransactionAdapter(myAdaptee);
93          }
94          
95          return theTransactionAdapter.getTarget();
96      }
97      
98      //------------------------------------------------------------------------
99      @SuppressWarnings("unchecked")
100     public <E> E
101     find(Class<E> aPersistentClass, Object anId)
102     {
103         assert anId instanceof Serializable : "'anId' must be Serializable";
104         
105         E myElement = (E) getUnderlying().load(aPersistentClass, 
106             (Serializable) anId);
107         
108         return myElement;
109     }
110 
111     //------------------------------------------------------------------------
112     public Session
113     getDelegate()
114     {
115         return getUnderlying();
116     }
117     
118     //------------------------------------------------------------------------
119     public IQuery
120     createQuery(String aSqlString)
121     {
122         IQuery myQuery = new HibernateQueryAdapter(getUnderlying().createQuery(
123             aSqlString)).getTarget();
124         
125         return myQuery;
126     }
127     
128     //------------------------------------------------------------------------
129     public IQuery
130     createNativeQuery(String aSqlString, Class<?> aPersistentClass)
131     {
132         IQuery myQuery = new HibernateQueryAdapter(getUnderlying().
133             createSQLQuery(aSqlString)).getTarget();
134         
135         return myQuery;
136     }        
137     
138     //------------------------------------------------------------------------  
139     public void
140     remove(Object anObject)
141     {
142         getUnderlying().delete(anObject);
143     }
144     
145     //------------------------------------------------------------------------  
146     public void
147     update(Object anObject)
148     {
149         getUnderlying().saveOrUpdate(anObject);
150     }
151 
152     //------------------------------------------------------------------------  
153     @SuppressWarnings("unchecked")
154     public <Id> Id
155     persist(Object anObject)
156     {
157         return (Id) getUnderlying().save(anObject);
158     }
159     
160     //------------------------------------------------------------------------
161     // members
162     //------------------------------------------------------------------------
163     private IAdapter<ITransaction, Transaction> theTransactionAdapter;
164 }