×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: Maduka Jayawardana
Added: Sep 14, 2015 8:35 AM
Views: 1910
Tags: no tags
  1. // *************************************************************************************************
  2. //
  3. // PROJECT : SchwingSystem
  4. // ************************************************************************************************
  5. //
  6. // Copyright(C) 2015 Maduka Ashan Jayawardana
  7. // All rights reserved.
  8. //
  9. // THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF
  10. // Maduka Ashan Jayawardana.
  11. //
  12. //
  13. // part of this file may be reproduced or distributed in any form or by any
  14. // means without the written approval of the Maduka Ashan Jayawardana
  15. //
  16. // *************************************************************************************************
  17. //
  18. // REVISIONS:
  19. // Author : MadukaJ
  20. // Date : 8 Sep 2015
  21. // *************
  22. package com.schwing.pms.core.dao.impl;
  23.  
  24.  
  25. import java.io.Serializable;
  26. import java.lang.reflect.ParameterizedType;
  27. import java.util.List;
  28.  
  29. import javax.persistence.EntityManager;
  30. import javax.persistence.PersistenceContext;
  31. import javax.persistence.Query;
  32.  
  33. /**
  34.  * @author MadukaJ
  35.  * @date : 8 Sep 2015
  36.  * @Since : version 1.0
  37.  * @File : DaoImpl.java
  38.  * @Description :- This is a generic DAO which is responsible to perform all CRUD operations in
  39.  *              general manner,This is abstract class and all DAO needs to be inherited from this
  40.  *              dao to get the common CRUD features.
  41.  *              
  42.  * @param <T>
  43.  * @param <PK>
  44.  **/
  45.  
  46. public class DaoImpl<T, PK extends Serializable> {
  47.  
  48.   /* Persistence Entity class type. */
  49.   protected Class<T> entityClass;
  50.  
  51.   /* Entity manager to perform DAO operations. */
  52.   @PersistenceContext
  53.   protected EntityManager entityManager;
  54.  
  55.  
  56.  
  57.   /**
  58.    * Constructor
  59.    */
  60.   @SuppressWarnings("unchecked")
  61.   public DaoImpl() {
  62.    
  63.     ParameterizedType genericSuperclass = (ParameterizedType) getClass().getGenericSuperclass();
  64.     this.entityClass = (Class<T>) genericSuperclass.getActualTypeArguments()[0];
  65.   }
  66.  
  67.  
  68.   /*
  69.    * (non-Javadoc)
  70.    *
  71.    * @see com.fg.baking.core.dao.IDao#create(java.lang.Object)
  72.    */
  73.   public T create(T t) {
  74.    
  75.     this.entityManager.persist(t);
  76.     return t;
  77.   }
  78.  
  79.  
  80.   /*
  81.    * (non-Javadoc)
  82.    *
  83.    * @see com.fg.baking.core.dao.IDao#read(java.io.Serializable)
  84.    */
  85.   public T read(PK id) {
  86.    
  87.     return this.entityManager.find(entityClass, id);
  88.   }
  89.  
  90.  
  91.   /*
  92.    * (non-Javadoc)
  93.    *
  94.    * @see com.fg.baking.core.dao.IDao#update(java.lang.Object)
  95.    */
  96.   public T update(T t) {
  97.    
  98.     return this.entityManager.merge(t);
  99.   }
  100.  
  101.  
  102.   /*
  103.    * (non-Javadoc)
  104.    *
  105.    * @see com.fg.baking.core.dao.IDao#delete(java.lang.Object)
  106.    */
  107.   public void delete(T t) {
  108.    
  109.     t = this.entityManager.merge(t);
  110.     this.entityManager.remove(t);
  111.   }
  112.  
  113.  
  114.   /*
  115.    * (non-Javadoc)
  116.    *
  117.    * @see com.yummy.platform.spring.dao.BaseDao#findAll()
  118.    */
  119.   @SuppressWarnings("unchecked")
  120.   public List<T> findAll() {
  121.    
  122.     Query query = entityManager.createQuery("FROM " + entityClass.getName() + " c");
  123.     return (List<T>) query.getResultList();
  124.   }
  125.  
  126. }
  127.