Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.lspsystem.dao; import com.lspsystem.model.basic.IEnable; import com.mysql.jdbc.StringUtils; import org.hibernate.Session; import org.springframework.stereotype.Repository; import java.io.Serializable; import java.lang.reflect.ParameterizedType; import java.util.List; /** * * @author evgeny * * ? ? CRUD */ @Repository public class GenericCustomerDaoImpl<T extends IEnable, PK extends Serializable> extends CustomerDAOModel implements GenericDao<T, PK> { public T create(T newInstance) { //EntityManager em = getEntityManager(); try { System.out.println("Customer dao: persist: " + newInstance); newInstance.setEnable(true); em.persist(newInstance); } catch (Exception e) { e.printStackTrace(); } // em.flush(); return newInstance; } public T read(PK id) { //EntityManager em = getEntityManager(); return (T) em.find(getTypeClass(), id); } public void update(T transientObject) { //EntityManager em = getEntityManager(); em.merge(transientObject); } public void delete(T persistentObject) { //EntityManager em = getEntityManager(); persistentObject.setEnable(false); em.merge(persistentObject); } public boolean realDelete(PK id) { //EntityManager em = getEntityManager(); T object = this.read(id); boolean result = em.contains(object); if (result) { em.remove(object); } return result; } public List<T> getAll() { //EntityManager em = getEntityManager(); // System.err.println("FROM " + getTypeClass().getName()); // Session s = em.unwrap(Session.class); // if (enabled != null){ // s.enableFilter("byEnabled").setParameter("enabled", enabled); // } // if (substring != null){ // s.enableFilter("byNameSubstring").setParameter("substring", substring); // } return em.createQuery(("FROM " + getTypeClass().getName())).getResultList(); } @Override public List<T> getEnabled() { return em.createQuery(("FROM " + getTypeClass().getName() + " o WHERE o.enable = 1")).getResultList(); } @Override public List<T> getFiltered(Boolean enable, String search) { Session s = em.unwrap(Session.class); if (!StringUtils.isNullOrEmpty(search)) { s.enableFilter("byNameSubstring").setParameter("str", "%" + search.toLowerCase() + "%"); } if (enable != null) { s.enableFilter("byEnabled").setParameter("enabled", enable); } return s.createQuery("SELECT obj FROM " + getTypeClass().getName() + " obj").list(); } public boolean delete(PK id) { T object = this.read(id); boolean result = em.contains(object); if (result) { object.setEnable(false); em.merge(object); } return result; } @Override public boolean restore(PK id) { T obj = this.read(id); boolean result = em.contains(obj); if (result) { obj.setEnable(true); em.merge(obj); } return result; } @Override public void restore(T persistenceObject) { persistenceObject.setEnable(true); em.merge(persistenceObject); } private Class<?> getTypeClass() { Class<?> clazz = (Class<?>) ((ParameterizedType) this.getClass().getGenericSuperclass()) .getActualTypeArguments()[0]; return clazz; } }