buildframework/helium/sf/java/metadata/src/com/nokia/helium/metadata/JpaDAO.java
changeset 628 7c4a911dc066
equal deleted inserted replaced
588:c7c26511138f 628:7c4a911dc066
       
     1 /*
       
     2  * Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3  * All rights reserved.
       
     4  * This component and the accompanying materials are made available
       
     5  * under the terms of the License "Eclipse Public License v1.0"
       
     6  * which accompanies this distribution, and is available
       
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8  *
       
     9  * Initial Contributors:
       
    10  * Nokia Corporation - initial contribution.
       
    11  *
       
    12  * Contributors:
       
    13  *
       
    14  * Description:  
       
    15  *
       
    16  */
       
    17 package com.nokia.helium.metadata;
       
    18 
       
    19 import java.io.Serializable;
       
    20 import java.lang.reflect.ParameterizedType;
       
    21 import java.util.List;
       
    22 
       
    23 import javax.persistence.EntityManager;
       
    24 import javax.persistence.PersistenceContext;
       
    25 
       
    26 /**
       
    27  * Abstract class which implement the DAO pattern for the JPA
       
    28  * API. 
       
    29  *
       
    30  * @param <T> implementing the DAO for T.
       
    31  */
       
    32 public abstract class JpaDAO<T> implements DAO<T> {
       
    33     
       
    34     private Class<T> entityBeanType;
       
    35    
       
    36     @PersistenceContext
       
    37     private EntityManager entityManager;
       
    38 
       
    39     /**
       
    40      * Default constructor. Does some basic internal configuration.
       
    41      */
       
    42     @SuppressWarnings("unchecked")
       
    43     public JpaDAO() {
       
    44         this.entityBeanType = (Class<T>) ((ParameterizedType) getClass()
       
    45                 .getGenericSuperclass()).getActualTypeArguments()[0];
       
    46     }
       
    47     
       
    48     /**
       
    49      * @param entityManager the entityManager to set
       
    50      */
       
    51     public void setEntityManager(EntityManager entityManager) {
       
    52         this.entityManager = entityManager;
       
    53     }
       
    54 
       
    55     /**
       
    56      * @return the entityManager
       
    57      */
       
    58     protected EntityManager getEntityManager() {
       
    59         return entityManager;
       
    60     }
       
    61     
       
    62     /**
       
    63      * {@inheritDoc}
       
    64      */
       
    65     public T findById(Serializable id) {
       
    66         return entityManager.find(getEntityBeanType(), id);
       
    67     }
       
    68    
       
    69     /**
       
    70      * {@inheritDoc}
       
    71      */
       
    72     public List<T> findAll() {
       
    73         return entityManager.createQuery("from " +
       
    74                 getEntityBeanType().getName(), getEntityBeanType()).getResultList();
       
    75     }
       
    76    
       
    77     /**
       
    78      * Get the class this object is implementing the DAO for.  
       
    79      * @return the class this object is implementing the DAO for.
       
    80      */
       
    81     protected Class<T> getEntityBeanType() {
       
    82         return entityBeanType;
       
    83     }
       
    84     
       
    85     /**
       
    86      * {@inheritDoc}
       
    87      */
       
    88     public void persist(T data) {
       
    89         entityManager.persist(data);
       
    90     }
       
    91 
       
    92     /**
       
    93      * {@inheritDoc}
       
    94      */
       
    95     public void remove(T data) {
       
    96         entityManager.remove(data);
       
    97     }
       
    98    
       
    99 }
       
   100