Clover Coverage Report - perfectjpattern(Aggregated)
Coverage timestamp: Sat Feb 28 2009 14:35:07 CET
../../../../../img/srcFileCovDistChart9.png 84% of files have more coverage
17   164   13   1.7
2   88   0.76   10
10     1.3  
1    
9.4% of code in this file is excluded from these metrics.
 
  HibernateSessionAdapter       Line # 39 17 9.4% 13 3 89.7% 0.8965517
 
  (14)
 
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  11 toggle public
53    HibernateSessionAdapter(Session anAdaptee)
54    throws IllegalArgumentException
55    {
56  11 super(ISession.class, anAdaptee);
57    }
58   
59    //------------------------------------------------------------------------
60    // protected
61    //------------------------------------------------------------------------
62    /**
63    * {@inheritDoc}
64    */
 
65  3 toggle @Override
66    protected Object
67    invokeUnderlying(Method aMethod, Object[] anArguments)
68    throws Throwable
69    {
70  3 try
71    {
72  3 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  59 toggle public ITransaction
85    getTransaction()
86    {
87  59 Transaction myAdaptee = getUnderlying().getTransaction();
88   
89  59 if (theTransactionAdapter == null || theTransactionAdapter.
90    getAdaptee() != myAdaptee)
91    {
92  11 theTransactionAdapter = new HibernateTransactionAdapter(myAdaptee);
93    }
94   
95  59 return theTransactionAdapter.getTarget();
96    }
97   
98    //------------------------------------------------------------------------
 
99  3 toggle @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  3 E myElement = (E) getUnderlying().load(aPersistentClass,
106    (Serializable) anId);
107   
108  3 return myElement;
109    }
110   
111    //------------------------------------------------------------------------
 
112  9 toggle public Session
113    getDelegate()
114    {
115  9 return getUnderlying();
116    }
117   
118    //------------------------------------------------------------------------
 
119  14 toggle public IQuery
120    createQuery(String aSqlString)
121    {
122  14 IQuery myQuery = new HibernateQueryAdapter(getUnderlying().createQuery(
123    aSqlString)).getTarget();
124   
125  14 return myQuery;
126    }
127   
128    //------------------------------------------------------------------------
 
129  0 toggle public IQuery
130    createNativeQuery(String aSqlString, Class<?> aPersistentClass)
131    {
132  0 IQuery myQuery = new HibernateQueryAdapter(getUnderlying().
133    createSQLQuery(aSqlString)).getTarget();
134   
135  0 return myQuery;
136    }
137   
138    //------------------------------------------------------------------------
 
139  1 toggle public void
140    remove(Object anObject)
141    {
142  1 getUnderlying().delete(anObject);
143    }
144   
145    //------------------------------------------------------------------------
 
146  3 toggle public void
147    update(Object anObject)
148    {
149  3 getUnderlying().saveOrUpdate(anObject);
150    }
151   
152    //------------------------------------------------------------------------
 
153  18 toggle @SuppressWarnings("unchecked")
154    public <Id> Id
155    persist(Object anObject)
156    {
157  18 return (Id) getUnderlying().save(anObject);
158    }
159   
160    //------------------------------------------------------------------------
161    // members
162    //------------------------------------------------------------------------
163    private IAdapter<ITransaction, Transaction> theTransactionAdapter;
164    }