Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package th.athichitsakul.dao.impl;
- import java.lang.reflect.ParameterizedType;
- import java.util.List;
- import th.athichitsakul.dao.DAO;
- import th.athichitsakul.dao.StandardDAO;
- public abstract class StandardDAOImpl<T> implements StandardDAO<T>{
- Class<T> objectClass;
- protected DAO DAO = null;
- public StandardDAOImpl(String puID) {
- this.DAO = new DAO(puID);
- objectClass = (Class<T>)((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
- }
- public void save(T object)throws Exception{
- try{
- DAO.begin();
- DAO.save(object);
- }catch(Exception e){
- if (DAO.getTransaction().isActive()) {
- DAO.getTransaction().rollback();
- }
- throw e;
- }finally{
- DAO.close();
- }
- }
- public void remove(Object primaryKey)throws Exception{
- try{
- DAO.begin();
- Object object = DAO.getEntityManager().find(objectClass, primaryKey);
- if(object==null) throw new NullPointerException("Removing Object Not Found");
- DAO.getEntityManager().remove(object);
- }catch(Exception e){
- throw e;
- }finally{
- DAO.close();
- }
- }
- public T find(Object primaryKey) throws Exception{
- T queryResult = null;
- try{
- DAO.begin();
- queryResult = DAO.getEntityManager().find(objectClass, primaryKey);
- }catch(Exception e){
- throw e;
- }finally{
- DAO.close();
- }
- return queryResult;
- }
- public List<T> list() throws Exception{
- List<T> queryResult = null;
- try{
- DAO.begin();
- queryResult = DAO.getEntityManager().createQuery("FROM ".concat(objectClass.getName() )).getResultList();
- }catch(Exception e){
- throw e;
- }finally{
- DAO.close();
- }
- return queryResult;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement