// *************************************************************************************************
//
// PROJECT : SchwingSystem
// ************************************************************************************************
//
// Copyright(C) 2015 Maduka Ashan Jayawardana
// All rights reserved.
//
// THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF
// Maduka Ashan Jayawardana.
//
//
// part of this file may be reproduced or distributed in any form or by any
// means without the written approval of the Maduka Ashan Jayawardana
//
// *************************************************************************************************
//
// REVISIONS:
// Author : MadukaJ
// Date : 8 Sep 2015
// *************
package com.schwing.pms.core.dao.impl;
import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
/**
* @author MadukaJ
* @date : 8 Sep 2015
* @Since : version 1.0
* @File : DaoImpl.java
* @Description :- This is a generic DAO which is responsible to perform all CRUD operations in
* general manner,This is abstract class and all DAO needs to be inherited from this
* dao to get the common CRUD features.
*
* @param <T>
* @param <PK>
**/
public class DaoImpl<T, PK extends Serializable> {
/* Persistence Entity class type. */
protected Class<T> entityClass;
/* Entity manager to perform DAO operations. */
@PersistenceContext
protected EntityManager entityManager;
/**
* Constructor
*/
@SuppressWarnings("unchecked")
public DaoImpl() {
ParameterizedType genericSuperclass = (ParameterizedType) getClass().getGenericSuperclass();
this.entityClass = (Class<T>) genericSuperclass.getActualTypeArguments()[0];
}
/*
* (non-Javadoc)
*
* @see com.fg.baking.core.dao.IDao#create(java.lang.Object)
*/
public T create(T t) {
this.entityManager.persist(t);
return t;
}
/*
* (non-Javadoc)
*
* @see com.fg.baking.core.dao.IDao#read(java.io.Serializable)
*/
public T read(PK id) {
return this.entityManager.find(entityClass, id);
}
/*
* (non-Javadoc)
*
* @see com.fg.baking.core.dao.IDao#update(java.lang.Object)
*/
public T update(T t) {
return this.entityManager.merge(t);
}
/*
* (non-Javadoc)
*
* @see com.fg.baking.core.dao.IDao#delete(java.lang.Object)
*/
public void delete(T t) {
t = this.entityManager.merge(t);
this.entityManager.remove(t);
}
/*
* (non-Javadoc)
*
* @see com.yummy.platform.spring.dao.BaseDao#findAll()
*/
@SuppressWarnings("unchecked")
public List<T> findAll() {
Query query = entityManager.createQuery("FROM " + entityClass.getName() + " c");
return (List<T>) query.getResultList();
}
}