Changeset 409 for tapas/persistence


Ignore:
Timestamp:
03/14/12 14:52:29 (12 years ago)
Author:
vmipsl
Message:

backoffice

Location:
tapas/persistence
Files:
4 added
5 edited
3 copied

Legend:

Unmodified
Added
Removed
  • tapas/persistence/implementation/com/ether/dao/DomainAccessObjectImpl.java

    r376 r409  
    11package com.ether.dao; 
    22 
    3 import static com.ether.PersistenceHelper.convert; 
    4 import static com.ether.PersistenceHelper.uniqueResult; 
     3import com.ether.LikeExpression; 
     4import com.ether.PersistenceException; 
     5import com.ether.PersistenceHelper; 
     6import org.hibernate.Criteria; 
     7import org.hibernate.Session; 
     8import org.hibernate.SessionFactory; 
     9import org.hibernate.criterion.Criterion; 
     10import org.hibernate.criterion.DetachedCriteria; 
     11import org.hibernate.criterion.Example; 
     12import org.hibernate.criterion.MatchMode; 
     13import org.hibernate.criterion.Projections; 
     14import org.jetbrains.annotations.NotNull; 
     15import org.jetbrains.annotations.Nullable; 
     16import org.springframework.dao.DataAccessException; 
     17import org.springframework.transaction.annotation.Propagation; 
     18import org.springframework.transaction.annotation.Transactional; 
    519 
    620import java.io.Serializable; 
     
    1024import java.util.List; 
    1125 
    12 import org.hibernate.Criteria; 
    13 import org.hibernate.Session; 
    14 import org.hibernate.SessionFactory; 
    15 import org.hibernate.criterion.DetachedCriteria; 
    16 import org.hibernate.criterion.Example; 
    17 import org.hibernate.criterion.Projections; 
    18 import org.jetbrains.annotations.NotNull; 
    19 import org.jetbrains.annotations.Nullable; 
    20 import org.springframework.dao.DataAccessException; 
    21  
    22 import com.ether.PersistenceException; 
     26import static com.ether.PersistenceHelper.convert; 
     27import static com.ether.PersistenceHelper.uniqueResult; 
    2328 
    2429public class DomainAccessObjectImpl<DO, PK extends Serializable> 
    25         implements DomainAccessObject<DO, PK> 
     30        implements DomainAccessObject<DO, PK> 
    2631{ 
    27     public DomainAccessObjectImpl(final Class<DO> domainObjectClass) { 
    28         _domainObjectClass = domainObjectClass; 
     32    public DomainAccessObjectImpl( final Class<DO> domainObjectClass ) 
     33    { 
     34        _domainObjectClass = domainObjectClass; 
     35    } 
     36 
     37    public DomainAccessObjectImpl( final Class<DO> domainObjectClass, final Class<PK> primaryKeyClass ) 
     38    { 
     39        _domainObjectClass = domainObjectClass; 
     40        _primaryKeyClass = primaryKeyClass; 
    2941    } 
    3042 
     
    3345    // ************************************************************************** // 
    3446 
    35     // ******************************* SELECT BY CRITERIA **//     
     47    // ******************************* SELECT BY CRITERIA **// 
    3648    @Nullable 
    37     public final DO selectByCriteria(final DetachedCriteria criteria) 
    38             throws PersistenceException 
    39     { 
    40         return selectByCriteria(getDomainObjectClass(), criteria); 
     49    public final DO selectByCriteria( final DetachedCriteria criteria ) 
     50            throws PersistenceException 
     51    { 
     52        return selectByCriteria( getDomainObjectClass(), criteria ); 
    4153    } 
    4254 
    4355    @Nullable 
    44     public final <O> O selectByCriteria(final Class<O> objectClass, final DetachedCriteria criteria) throws PersistenceException 
    45     { 
    46         return (O) criteria.getExecutableCriteria(_sessionFactory.getCurrentSession()).uniqueResult();   
     56    public final <O> O selectByCriteria( final Class<O> objectClass, final DetachedCriteria criteria ) 
     57            throws PersistenceException 
     58    { 
     59        return (O) criteria.getExecutableCriteria( _sessionFactory.getCurrentSession() ).uniqueResult(); 
    4760    } 
    4861 
    4962    @NotNull 
    50     public final <O> List<O> selectAllByCriteria(final Class<O> objectClass, final DetachedCriteria criteria) throws PersistenceException 
    51     { 
    52         final List<O> result =  (List<O>) criteria.getExecutableCriteria(_sessionFactory.getCurrentSession()).list(); 
    53         return null != result ? result : new ArrayList<O>(); 
     63    public final <O> List<O> selectAllByCriteria( final Class<O> objectClass, final DetachedCriteria criteria ) 
     64            throws PersistenceException 
     65    { 
     66        final List<O> result = (List<O>) criteria.getExecutableCriteria( _sessionFactory.getCurrentSession() ).list(); 
     67        return null != result ? result : new ArrayList<O>(); 
    5468    } 
    5569 
    5670    @NotNull 
    57     public final List<DO> selectAllByCriteria(final DetachedCriteria criteria) 
    58             throws PersistenceException 
    59     { 
    60         return selectAllByCriteria(getDomainObjectClass(), criteria); 
    61     } 
    62  
    63      
    64     // ******************************* INSERT **//     
     71    public final List<DO> selectAllByCriteria( final DetachedCriteria criteria ) 
     72            throws PersistenceException 
     73    { 
     74        return selectAllByCriteria( getDomainObjectClass(), criteria ); 
     75    } 
     76 
     77    @Transactional(propagation = Propagation.MANDATORY) 
     78    public final <O> O execute( final PersistenceHelper.Operation<O> operation ) 
     79            throws PersistenceException 
     80    { 
     81        return PersistenceHelper.execute( operation, _sessionFactory ); 
     82    } 
     83 
     84    // ******************************* INSERT **// 
    6585    @NotNull 
    66     public final PK insert(final DO domainObject) throws PersistenceException 
    67     { 
    68         try 
    69         { 
    70             final Session session = _sessionFactory.getCurrentSession(); 
    71             final PK primaryKey = (PK) session.save(domainObject); 
    72             session.flush(); 
    73             return primaryKey; 
    74         } 
    75         catch (DataAccessException e) 
    76         { 
    77             throw convert(e); 
    78         } 
    79     } 
    80  
    81     public final void insertAll(@NotNull final Collection<DO> domainObjects) 
    82             throws PersistenceException 
    83     { 
    84         try 
    85         { 
    86             final Session session = _sessionFactory.getCurrentSession(); 
    87             int i = 0; 
    88             for (final DO domainObject : domainObjects) 
    89             { 
    90                 session.save(domainObject); 
    91                 // Avoid OutOfMemoryError 
    92                 if (i >= 500) 
    93                 { 
    94                     session.flush(); 
    95                     session.clear(); 
    96                     i = 0; 
    97                 } 
    98             } 
    99             session.flush(); 
    100         } 
    101         catch (DataAccessException e) 
    102         { 
    103             throw convert(e); 
    104         } 
    105     } 
    106  
    107     // ******************************* UPDATE **//     
    108     public void update(@NotNull final DO domainObject) 
    109             throws PersistenceException 
    110     { 
    111         try 
    112         { 
    113             final Session session = _sessionFactory.getCurrentSession(); 
    114             session.update(domainObject); 
    115             session.flush(); 
    116         } 
    117         catch (DataAccessException e) 
    118         { 
    119             throw convert(e); 
    120         } 
    121     } 
    122  
    123     public final void updateAll(final Collection<? extends DO> domainObjects) 
    124             throws PersistenceException 
    125     { 
    126         try 
    127         { 
    128             final Session session = _sessionFactory.getCurrentSession(); 
    129             for (final DO domainObject : domainObjects) 
    130                 session.saveOrUpdate(domainObject); 
    131             session.flush(); 
    132         } 
    133         catch (DataAccessException e) 
    134         { 
    135             throw convert(e); 
    136         } 
    137     } 
    138  
    139     // ******************************* UPGRADE **//     
    140     public final void upgrade(final DO domainObject) 
    141             throws PersistenceException 
    142     { 
    143         try 
    144         { 
    145             final Session session = _sessionFactory.getCurrentSession(); 
    146             session.refresh(domainObject); 
    147             session.flush(); 
    148         } 
    149         catch (DataAccessException e) 
    150         { 
    151             throw convert(e); 
    152         } 
    153     } 
    154  
    155     public final void upgradeAll(final Collection<DO> domainObjects) 
    156             throws PersistenceException 
    157     { 
    158         try 
    159         { 
    160             final Session session = _sessionFactory.getCurrentSession(); 
    161             for (final DO domainObject : domainObjects) 
    162             { 
    163                 session.refresh(domainObject); 
    164             } 
    165             session.flush(); 
    166         } 
    167         catch (DataAccessException e) 
    168         { 
    169             throw convert(e); 
    170         } 
    171     } 
    172  
    173     // ******************************* DELETE **//     
    174     public void deleteByPrimaryKey(final PK primaryKey) 
    175             throws PersistenceException 
    176     { 
    177         try 
    178         { 
    179             final Session session = _sessionFactory.getCurrentSession(); 
    180             final Object domainObject = session.get(getDomainObjectClass(), primaryKey); 
    181             if(null != domainObject) 
    182                 session.delete(domainObject); 
    183             else 
    184                 throw new PersistenceException(PersistenceException.PersistenceCode.OBJECT_TO_DELETE_NOT_FOUND); 
    185             session.flush(); 
    186         } 
    187         catch (DataAccessException e) 
    188         { 
    189             throw convert(e); 
    190         } 
    191     } 
    192  
    193     public final void delete(@NotNull final DO domainObject) 
    194             throws PersistenceException 
    195     { 
    196         try 
    197         { 
    198             final Session session = _sessionFactory.getCurrentSession(); 
    199             session.delete(domainObject); 
    200             session.flush(); 
    201         } 
    202         catch (DataAccessException e) 
    203         { 
    204             throw convert(e); 
    205         } 
    206     } 
    207  
    208     public final void deleteAll(final Collection<DO> domainObjects) 
    209             throws PersistenceException 
    210     { 
    211         try 
    212         { 
    213             final Session session = _sessionFactory.getCurrentSession(); 
    214             for (final DO domainObject : domainObjects) 
    215                 session.delete(domainObject); 
    216             session.flush(); 
    217         } 
    218         catch (DataAccessException e) 
    219         { 
    220             throw convert(e); 
    221         } 
    222     } 
    223  
    224     // ******************************* SELECT **//     
    225     public final DO selectByPrimaryKey(@NotNull final PK primaryKey) 
    226             throws PersistenceException 
    227     { 
    228         try 
    229         { 
    230             final Session session = _sessionFactory.getCurrentSession(); 
    231             return (DO) session.get(getDomainObjectClass(), primaryKey); 
    232         } 
    233         catch (DataAccessException e) 
    234         { 
    235             throw convert(e); 
    236         } 
    237     } 
    238  
    239     public final DO selectByExample(final DO domainObject) 
    240             throws PersistenceException 
    241     { 
    242         final Session session = _sessionFactory.getCurrentSession(); 
    243         final Criteria executableCriteria = session.createCriteria(domainObject.getClass()); 
    244         executableCriteria.add(Example.create(domainObject)); 
    245         return uniqueResult((Collection<DO>) executableCriteria.list()); 
    246     } 
    247  
    248     public final List<DO> selectAllByExample(final DO domainObject) 
    249             throws PersistenceException 
    250     { 
    251         final Session session = _sessionFactory.getCurrentSession(); 
    252         final Criteria executableCriteria = session.createCriteria(domainObject.getClass()); 
    253         executableCriteria.add(Example.create(domainObject)); 
    254         return (List<DO>) executableCriteria.list(); 
    255     } 
    256  
    257     public final List<DO> selectAll() throws PersistenceException 
    258     { 
    259         try 
    260         { 
    261             final Session session = _sessionFactory.getCurrentSession(); 
    262             final Criteria executableCriteria = session.createCriteria(getDomainObjectClass());      
    263             return (List<DO>) executableCriteria.list(); 
    264         } 
    265         catch (DataAccessException e) 
    266         { 
    267             throw convert(e); 
    268         } 
     86    public final PK insert( final DO domainObject ) 
     87            throws PersistenceException 
     88    { 
     89        try 
     90        { 
     91            final Session session = _sessionFactory.getCurrentSession(); 
     92            final PK primaryKey = (PK) session.save( domainObject ); 
     93            session.flush(); 
     94            return primaryKey; 
     95        } 
     96        catch( DataAccessException e ) 
     97        { 
     98            throw convert( e ); 
     99        } 
     100    } 
     101 
     102    public final void insertAll( @NotNull final Collection<DO> domainObjects ) 
     103            throws PersistenceException 
     104    { 
     105        try 
     106        { 
     107            final Session session = _sessionFactory.getCurrentSession(); 
     108            int i = 0; 
     109            for( final DO domainObject : domainObjects ) 
     110            { 
     111                session.save( domainObject ); 
     112                // Avoid OutOfMemoryError 
     113                if( i >= 500 ) 
     114                { 
     115                    session.flush(); 
     116                    session.clear(); 
     117                    i = 0; 
     118                } 
     119            } 
     120            session.flush(); 
     121        } 
     122        catch( DataAccessException e ) 
     123        { 
     124            throw convert( e ); 
     125        } 
     126    } 
     127 
     128    // ******************************* UPDATE **// 
     129    public void update( @NotNull final DO domainObject ) 
     130            throws PersistenceException 
     131    { 
     132        try 
     133        { 
     134            final Session session = _sessionFactory.getCurrentSession(); 
     135            session.update( domainObject ); 
     136            session.flush(); 
     137        } 
     138        catch( DataAccessException e ) 
     139        { 
     140            throw convert( e ); 
     141        } 
     142    } 
     143 
     144    public final void updateAll( final Collection<? extends DO> domainObjects ) 
     145            throws PersistenceException 
     146    { 
     147        try 
     148        { 
     149            final Session session = _sessionFactory.getCurrentSession(); 
     150            for( final DO domainObject : domainObjects ) 
     151                session.saveOrUpdate( domainObject ); 
     152            session.flush(); 
     153        } 
     154        catch( DataAccessException e ) 
     155        { 
     156            throw convert( e ); 
     157        } 
     158    } 
     159 
     160    // ******************************* UPGRADE **// 
     161    public final void upgrade( final DO domainObject ) 
     162            throws PersistenceException 
     163    { 
     164        try 
     165        { 
     166            final Session session = _sessionFactory.getCurrentSession(); 
     167            session.refresh( domainObject ); 
     168            session.flush(); 
     169        } 
     170        catch( DataAccessException e ) 
     171        { 
     172            throw convert( e ); 
     173        } 
     174    } 
     175 
     176    public final void upgradeAll( final Collection<DO> domainObjects ) 
     177            throws PersistenceException 
     178    { 
     179        try 
     180        { 
     181            final Session session = _sessionFactory.getCurrentSession(); 
     182            for( final DO domainObject : domainObjects ) 
     183            { 
     184                session.refresh( domainObject ); 
     185            } 
     186            session.flush(); 
     187        } 
     188        catch( DataAccessException e ) 
     189        { 
     190            throw convert( e ); 
     191        } 
     192    } 
     193 
     194    // ******************************* DELETE **// 
     195    public void deleteByPrimaryKey( final PK primaryKey ) 
     196            throws PersistenceException 
     197    { 
     198        try 
     199        { 
     200            final Session session = _sessionFactory.getCurrentSession(); 
     201            final Object domainObject = session.get( getDomainObjectClass(), primaryKey ); 
     202            if( null != domainObject ) 
     203                session.delete( domainObject ); 
     204            else 
     205                throw new PersistenceException( PersistenceException.PersistenceCode.OBJECT_TO_DELETE_NOT_FOUND ); 
     206            session.flush(); 
     207        } 
     208        catch( DataAccessException e ) 
     209        { 
     210            throw convert( e ); 
     211        } 
     212    } 
     213 
     214    public final void delete( @NotNull final DO domainObject ) 
     215            throws PersistenceException 
     216    { 
     217        try 
     218        { 
     219            final Session session = _sessionFactory.getCurrentSession(); 
     220            session.delete( domainObject ); 
     221            session.flush(); 
     222        } 
     223        catch( DataAccessException e ) 
     224        { 
     225            throw convert( e ); 
     226        } 
     227    } 
     228 
     229    public final void deleteAll( final Collection<DO> domainObjects ) 
     230            throws PersistenceException 
     231    { 
     232        try 
     233        { 
     234            final Session session = _sessionFactory.getCurrentSession(); 
     235            for( final DO domainObject : domainObjects ) 
     236                session.delete( domainObject ); 
     237            session.flush(); 
     238        } 
     239        catch( DataAccessException e ) 
     240        { 
     241            throw convert( e ); 
     242        } 
     243    } 
     244 
     245    // ******************************* SELECT **// 
     246    public final DO selectByPrimaryKey( @NotNull final PK primaryKey ) 
     247            throws PersistenceException 
     248    { 
     249        try 
     250        { 
     251            final Session session = _sessionFactory.getCurrentSession(); 
     252            return (DO) session.get( getDomainObjectClass(), primaryKey ); 
     253        } 
     254        catch( DataAccessException e ) 
     255        { 
     256            throw convert( e ); 
     257        } 
     258    } 
     259 
     260    public final DO selectByExample( final DO domainObject ) 
     261            throws PersistenceException 
     262    { 
     263        final Session session = _sessionFactory.getCurrentSession(); 
     264        final Criteria executableCriteria = session.createCriteria( domainObject.getClass() ); 
     265        executableCriteria.add( Example.create( domainObject ) ); 
     266        return uniqueResult( (Collection<DO>) executableCriteria.list() ); 
     267    } 
     268 
     269    public final List<DO> selectAllByExample( final DO domainObject ) 
     270            throws PersistenceException 
     271    { 
     272        final Session session = _sessionFactory.getCurrentSession(); 
     273        final Criteria executableCriteria = session.createCriteria( domainObject.getClass() ); 
     274        executableCriteria.add( Example.create( domainObject ) ); 
     275        return (List<DO>) executableCriteria.list(); 
     276    } 
     277 
     278    public final List<DO> selectAll() 
     279            throws PersistenceException 
     280    { 
     281        try 
     282        { 
     283            final Session session = _sessionFactory.getCurrentSession(); 
     284            final Criteria executableCriteria = session.createCriteria( getDomainObjectClass() ); 
     285            return (List<DO>) executableCriteria.list(); 
     286        } 
     287        catch( DataAccessException e ) 
     288        { 
     289            throw convert( e ); 
     290        } 
    269291    } 
    270292 
    271293    // ******************************* PAGINATE **// 
    272     protected <T> List<T> selectPage(@Nullable final Integer firstResult, @Nullable final Integer maxResults, final DetachedCriteria detachedCriteria) 
    273             throws PersistenceException 
    274     { 
    275         final Session session = _sessionFactory.getCurrentSession(); 
    276         final Criteria criteria = detachedCriteria.getExecutableCriteria(session); 
    277  
    278         if (null != maxResults) 
    279         { 
    280             criteria.setMaxResults(maxResults); 
    281             if (null != firstResult) 
    282                 criteria.setFirstResult(firstResult); 
    283         } 
    284         return (List<T>) criteria.list(); 
    285     } 
    286  
    287     protected Integer count(final DetachedCriteria detachedCriteria) 
    288             throws PersistenceException 
    289     { 
    290         final Session session = _sessionFactory.getCurrentSession(); 
    291         final Criteria executableCriteria = detachedCriteria.getExecutableCriteria(session); 
    292         executableCriteria.setProjection(Projections.rowCount()); 
    293         return (Integer) executableCriteria.uniqueResult(); 
    294     } 
     294    protected <T> List<T> selectPage( @Nullable final Integer firstResult, @Nullable final Integer maxResults, final DetachedCriteria detachedCriteria ) 
     295            throws PersistenceException 
     296    { 
     297        final Session session = _sessionFactory.getCurrentSession(); 
     298        final Criteria criteria = detachedCriteria.getExecutableCriteria( session ); 
     299 
     300        if( null != maxResults ) 
     301        { 
     302            criteria.setMaxResults( maxResults ); 
     303            if( null != firstResult ) 
     304                criteria.setFirstResult( firstResult ); 
     305        } 
     306        return (List<T>) criteria.list(); 
     307    } 
     308 
     309    protected Integer count( final DetachedCriteria detachedCriteria ) 
     310            throws PersistenceException 
     311    { 
     312        final Session session = _sessionFactory.getCurrentSession(); 
     313        final Criteria executableCriteria = detachedCriteria.getExecutableCriteria( session ); 
     314        executableCriteria.setProjection( Projections.rowCount() ); 
     315        if( executableCriteria.uniqueResult() instanceof Long ) 
     316            return ( (Long) executableCriteria.uniqueResult() ).intValue(); 
     317        else 
     318            return (Integer) executableCriteria.uniqueResult(); 
     319 
     320    } 
     321 
     322    // ******************************* SELECT **// 
     323    public static Criterion escapedLike( final String fieldName, final String value ) 
     324    { 
     325        return new LikeExpression( fieldName, value, MatchMode.ANYWHERE, null, true ); 
     326    } 
     327 
    295328    // ************************************************************************** // 
    296329    // ************************************************************************** // 
    297      
     330 
    298331    @SuppressWarnings("unchecked") 
    299332    public final Class<DO> getDomainObjectClass() 
    300333    { 
    301         if (_domainObjectClass == null) 
    302         { 
    303             if (getClass().getGenericSuperclass() instanceof ParameterizedType) 
    304                 _domainObjectClass = (Class<DO>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]; 
    305             else 
    306                 throw new IllegalArgumentException("domain class is not null and DAO is not parameterized !"); 
    307         } 
    308  
    309         return _domainObjectClass; 
    310     } 
    311  
    312     public final void setDomainObjectClass(final Class<DO> domainObjectClass) 
    313     { 
    314         _domainObjectClass = domainObjectClass; 
    315     } 
    316  
    317     public final void setSessionFactory(final SessionFactory sessionFactory) 
    318     { 
    319         _sessionFactory = sessionFactory; 
     334        if( _domainObjectClass == null ) 
     335        { 
     336            if( getClass().getGenericSuperclass() instanceof ParameterizedType ) 
     337                _domainObjectClass = (Class<DO>) ( (ParameterizedType) getClass().getGenericSuperclass() ).getActualTypeArguments()[0]; 
     338            else 
     339                throw new IllegalArgumentException( "domain class is not null and DAO is not parameterized !" ); 
     340        } 
     341 
     342        return _domainObjectClass; 
     343    } 
     344 
     345    public final void setDomainObjectClass( final Class<DO> domainObjectClass ) 
     346    { 
     347        _domainObjectClass = domainObjectClass; 
     348    } 
     349 
     350    public final Class<PK> getPrimaryKeyClass() 
     351    { 
     352        if( _primaryKeyClass == null ) 
     353        { 
     354            if( getClass().getGenericSuperclass() instanceof ParameterizedType ) 
     355                _primaryKeyClass = (Class<PK>) ( (ParameterizedType) getClass().getGenericSuperclass() ).getActualTypeArguments()[1]; 
     356            else 
     357                throw new IllegalArgumentException( "primary key class is not null and DAO is not parameterized !" ); 
     358        } 
     359 
     360        return _primaryKeyClass; 
     361    } 
     362 
     363    public final void setPrimaryKeyClass( final Class<PK> primaryKeyClass ) 
     364    { 
     365        _primaryKeyClass = primaryKeyClass; 
     366    } 
     367 
     368    public final void setSessionFactory( final SessionFactory sessionFactory ) 
     369    { 
     370        _sessionFactory = sessionFactory; 
    320371    } 
    321372 
    322373    public final SessionFactory getSessionFactory() 
    323374    { 
    324         return _sessionFactory; 
     375        return _sessionFactory; 
    325376    } 
    326377 
     
    329380    private SessionFactory _sessionFactory; 
    330381    private Class<DO> _domainObjectClass; 
     382    private Class<PK> _primaryKeyClass; 
    331383} 
  • tapas/persistence/implementation/dao-context.xml

    r378 r409  
    44<!-- Application context DAO layer --> 
    55<beans> 
    6         <bean id="refPlateformDAO" class="com.ether.dao.PlateformDAOImpl"> 
    7                 <property name="sessionFactory"> 
    8                         <ref bean="sessionFactory" /> 
    9                 </property> 
    10         </bean> 
     6    <bean id="refPlateformDAO" class="com.ether.dao.PlateformDAOImpl"> 
     7        <property name="sessionFactory"> 
     8            <ref bean="sessionFactory"/> 
     9        </property> 
     10    </bean> 
    1111 
    1212    <bean id="refParameterDAO" class="com.ether.dao.ParameterDAOImpl"> 
    1313        <property name="sessionFactory"> 
    14             <ref bean="sessionFactory" /> 
     14            <ref bean="sessionFactory"/> 
     15        </property> 
     16    </bean> 
     17 
     18    <bean id="refUserDAO" class="com.ether.dao.user.UserDAOImpl"> 
     19        <property name="sessionFactory"> 
     20            <ref bean="sessionFactory"/> 
    1521        </property> 
    1622    </bean> 
  • tapas/persistence/implementation/hibernate-domain.cfg.xml

    r378 r409  
    66<hibernate-configuration> 
    77    <session-factory> 
    8         <mapping resource="com/ether/dao/Plateform.hbm.xml"/> 
    9         <mapping resource="com/ether/dao/Parameter.hbm.xml"/> 
     8        <mapping resource="com/ether/dao/user/User.hbm.xml"/> 
    109    </session-factory> 
    1110</hibernate-configuration> 
  • tapas/persistence/implementation/hibernate.cfg.xml

    r378 r409  
    1717                                <prop key="hibernate.bytecode.use_reflection_optimizer">false</prop> 
    1818                                <prop key="hibernate.connection.driver_class">org.postgresql.Driver</prop> 
    19                                 <prop key="hibernate.connection.url">jdbc:postgresql://darkstar.ipslnet:5432/PROJECT_SOURCE</prop> 
     19                                <prop key="hibernate.connection.url">jdbc:postgresql://darkstar.ipslnet:5432/tapas_bdd</prop> 
    2020                                <prop key="hibernate.connection.username">postgres</prop> 
    2121                                <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop> 
  • tapas/persistence/interface/com/ether/PersistenceHelper.java

    r376 r409  
    11package com.ether; 
    2  
    3 import java.util.Collection; 
    4 import com.ether.PersistenceException; 
    52 
    63import org.apache.commons.logging.Log; 
    74import org.apache.commons.logging.LogFactory; 
     5import org.hibernate.HibernateException; 
     6import org.hibernate.Session; 
     7import org.hibernate.SessionFactory; 
    88import org.springframework.dao.CleanupFailureDataAccessException; 
    99import org.springframework.dao.DataAccessException; 
     10import org.springframework.transaction.annotation.Propagation; 
     11import org.springframework.transaction.annotation.Transactional; 
     12 
     13import java.util.Collection; 
    1014 
    1115public final class PersistenceHelper 
     
    1317    /** 
    1418     * Returns an unique result from a collection of results. 
    15      *  
     19     * 
    1620     * @throws PersistenceException : if more than one result is contained in the collection an exception is thrown. 
    1721     */ 
    18     public static <O> O uniqueResult(final Collection<O> results) 
    19             throws PersistenceException 
     22    public static <O> O uniqueResult( final Collection<O> results ) 
     23            throws PersistenceException 
    2024    { 
    21         if (results == null || results.isEmpty()) 
    22             return null; 
    23         else if (results.size() > 1) 
    24             throw new PersistenceException(PersistenceException.PersistenceCode.INVALID_API_USAGE, "unique result expected !"); 
    25         else 
    26             return results.iterator().next(); 
     25        if( results == null || results.isEmpty() ) 
     26            return null; 
     27        else if( results.size() > 1 ) 
     28            throw new PersistenceException( PersistenceException.PersistenceCode.INVALID_API_USAGE, "unique result expected !" ); 
     29        else 
     30            return results.iterator().next(); 
    2731    } 
    2832 
    29     public static <O> O firstResult(final Collection<O> results) 
     33    public static <O> O firstResult( final Collection<O> results ) 
    3034    { 
    31         if (results == null || results.isEmpty()) return null; 
    32         return results.iterator().next(); 
     35        if( results == null || results.isEmpty() ) return null; 
     36        return results.iterator().next(); 
    3337    } 
    34    
     38 
    3539    /** 
    3640     * Converts an exception to a DAO exception. 
    37      *  
     41     * 
    3842     * @param exception : the exception. 
    3943     * @return the DAO exception. 
    4044     */ 
    41     public static PersistenceException convert(final Exception exception) 
     45    public static PersistenceException convert( final Exception exception ) 
    4246    { 
    43         // Propagates the exception if exception is persistence exception 
    44         if (exception instanceof PersistenceException) 
    45             return (PersistenceException) exception; 
     47        // Propagates the exception if exception is persistence exception 
     48        if( exception instanceof PersistenceException ) 
     49            return (PersistenceException) exception; 
    4650 
    47         // Rewraps exception if data access exception 
    48         final PersistenceException.PersistenceCode code; 
     51        // Rewraps exception if data access exception 
     52        final PersistenceException.PersistenceCode code; 
    4953 
    50         if (exception instanceof DataAccessException) 
    51         { 
    52             if (exception instanceof CleanupFailureDataAccessException) 
    53             { 
    54                 code = PersistenceException.PersistenceCode.CLEANUP_FAILURE; 
    55             } 
    56             else if (exception instanceof org.springframework.dao.ConcurrencyFailureException) 
    57             { 
    58                 code = PersistenceException.PersistenceCode.CONCURRENCY_FAILURE; 
    59             } 
    60             else if (exception instanceof org.springframework.dao.DataAccessResourceFailureException) 
    61             { 
    62                 code = PersistenceException.PersistenceCode.RESOURCE_FAILURE; 
    63             } 
    64             else if (exception instanceof org.springframework.dao.DataIntegrityViolationException) 
    65             { 
    66                 code = PersistenceException.PersistenceCode.INTEGRITY_VIOLATION; 
    67             } 
    68             else if (exception instanceof org.springframework.dao.DataRetrievalFailureException) 
    69             { 
    70                 code = PersistenceException.PersistenceCode.RETRIEVAL_FAILURE; 
    71             } 
    72             else if (exception instanceof org.springframework.jdbc.datasource.lookup.DataSourceLookupFailureException) 
    73             { 
    74                 code = PersistenceException.PersistenceCode.DATA_SOURCE_LOOKUP_FAILURE; 
    75             } 
    76             else if (exception instanceof org.springframework.dao.InvalidDataAccessApiUsageException) 
    77             { 
    78                 code = PersistenceException.PersistenceCode.INVALID_API_USAGE; 
    79             } 
    80             else if (exception instanceof org.springframework.dao.PermissionDeniedDataAccessException) 
    81             { 
    82                 code = PersistenceException.PersistenceCode.PERMISSION_DENIED; 
    83             } 
    84             else 
    85             { 
    86                 code = PersistenceException.PersistenceCode.UNKNOWN_FAILURE; 
    87             } 
    88         } 
    89         else 
    90         { 
    91             code = PersistenceException.PersistenceCode.UNKNOWN_FAILURE; 
    92         } 
     54        if( exception instanceof DataAccessException ) 
     55        { 
     56            if( exception instanceof CleanupFailureDataAccessException ) 
     57            { 
     58                code = PersistenceException.PersistenceCode.CLEANUP_FAILURE; 
     59            } 
     60            else if( exception instanceof org.springframework.dao.ConcurrencyFailureException ) 
     61            { 
     62                code = PersistenceException.PersistenceCode.CONCURRENCY_FAILURE; 
     63            } 
     64            else if( exception instanceof org.springframework.dao.DataAccessResourceFailureException ) 
     65            { 
     66                code = PersistenceException.PersistenceCode.RESOURCE_FAILURE; 
     67            } 
     68            else if( exception instanceof org.springframework.dao.DataIntegrityViolationException ) 
     69            { 
     70                code = PersistenceException.PersistenceCode.INTEGRITY_VIOLATION; 
     71            } 
     72            else if( exception instanceof org.springframework.dao.DataRetrievalFailureException ) 
     73            { 
     74                code = PersistenceException.PersistenceCode.RETRIEVAL_FAILURE; 
     75            } 
     76            else if( exception instanceof org.springframework.jdbc.datasource.lookup.DataSourceLookupFailureException ) 
     77            { 
     78                code = PersistenceException.PersistenceCode.DATA_SOURCE_LOOKUP_FAILURE; 
     79            } 
     80            else if( exception instanceof org.springframework.dao.InvalidDataAccessApiUsageException ) 
     81            { 
     82                code = PersistenceException.PersistenceCode.INVALID_API_USAGE; 
     83            } 
     84            else if( exception instanceof org.springframework.dao.PermissionDeniedDataAccessException ) 
     85            { 
     86                code = PersistenceException.PersistenceCode.PERMISSION_DENIED; 
     87            } 
     88            else 
     89            { 
     90                code = PersistenceException.PersistenceCode.UNKNOWN_FAILURE; 
     91            } 
     92        } 
     93        else 
     94        { 
     95            code = PersistenceException.PersistenceCode.UNKNOWN_FAILURE; 
     96        } 
    9397 
    94         final StringBuilder message = new StringBuilder(); 
    95         message.append(exception.getMessage()); 
     98        final StringBuilder message = new StringBuilder(); 
     99        message.append( exception.getMessage() ); 
    96100 
    97         final Throwable cause = exception.getCause(); 
    98         if (null != cause) 
    99         { 
    100             message.append(" : ").append(cause.getMessage()); 
    101         } 
     101        final Throwable cause = exception.getCause(); 
     102        if( null != cause ) 
     103        { 
     104            message.append( " : " ).append( cause.getMessage() ); 
     105        } 
    102106 
    103         return new PersistenceException(code, message.toString(), 
    104                 exception); 
     107        return new PersistenceException( code, message.toString(), 
     108                exception ); 
    105109    } 
    106110 
    107     private static final Log LOGGER = LogFactory.getLog(PersistenceHelper.class); 
     111    @Transactional(propagation = Propagation.MANDATORY) 
     112    public static <O> O execute( final Operation<O> operation, final SessionFactory sessionFactory ) 
     113            throws PersistenceException 
     114    { 
     115        try 
     116        { 
     117            final Session session = sessionFactory.getCurrentSession(); 
     118            return operation.operate( session ); 
     119        } 
     120        catch( org.hibernate.exception.LockAcquisitionException e ) 
     121        { 
     122            LOGGER.fatal( e.getMessage(), e ); 
     123            throw convert( e ); 
     124        } 
     125        catch( DataAccessException e ) 
     126        { 
     127            throw convert( e ); 
     128        } 
     129        catch( HibernateException e ) 
     130        { 
     131            throw convert( e ); 
     132        } 
     133    } 
     134 
     135    public interface Operation<O> 
     136    { 
     137        /** 
     138         * Executes an operation on a session (delete, update, ...) with no return statement. 
     139         * 
     140         * @param session the session. 
     141         * @return the operation result. 
     142         * @throws org.hibernate.HibernateException 
     143         *                               if an hibernate exception occurs. 
     144         * @throws java.sql.SQLException if an SQL exception occurs. 
     145         * @throws PersistenceException  if a DAO exception is thrown. 
     146         */ 
     147        O operate( Session session ) 
     148                throws PersistenceException; 
     149    } 
     150 
     151    private static final Log LOGGER = LogFactory.getLog( PersistenceHelper.class ); 
    108152} 
  • tapas/persistence/interface/com/ether/dao/UserDAO.java

    r287 r409  
    44import com.ether.user.User; 
    55import com.ether.user.UserFilter; 
    6 import com.ether.user.UserState; 
    76import org.jetbrains.annotations.NotNull; 
    87import org.jetbrains.annotations.Nullable; 
     
    2221 
    2322    @NotNull 
    24     List<User> getUsersByState( @NotNull final UserState userState ) 
    25             throws PersistenceException; 
    26  
    27     @NotNull 
    2823    List<User> getAllUsersByNameOrder() 
    2924            throws PersistenceException; 
Note: See TracChangeset for help on using the changeset viewer.