Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.18 KB | None | 0 0
  1. package ts.mo.tele3.dao.core;
  2.  
  3. import org.springframework.transaction.annotation.Transactional;
  4.  
  5. import java.lang.reflect.ParameterizedType;
  6.  
  7. import javax.persistence.EntityManager;
  8. import javax.persistence.PersistenceContext;
  9. import javax.persistence.TypedQuery;
  10. import javax.persistence.criteria.CriteriaBuilder;
  11. import javax.persistence.criteria.CriteriaDelete;
  12. import javax.persistence.criteria.CriteriaQuery;
  13. import javax.persistence.criteria.Root;
  14.  
  15. /**
  16.  * Abstract class for generic CRUD operations on a repository for a specific type.
  17.  */
  18. @Transactional
  19. public abstract class CrudDao<T, ID> implements Dao<T, ID> {
  20.  
  21.     private Class<T> clazz;
  22.  
  23.     @PersistenceContext
  24.     protected EntityManager em;
  25.  
  26.     @SuppressWarnings("unchecked")
  27.     public CrudDao() {
  28.         // TODO figure out what that means
  29.         clazz = (Class<T>)
  30.             ((ParameterizedType)getClass().getGenericSuperclass())
  31.                 .getActualTypeArguments()[0];
  32.     }
  33.  
  34.     /**
  35.      * Saves a given entity.
  36.      */
  37.     public void save(T entity) {
  38.         em.persist(entity);
  39.     }
  40.  
  41.     /**
  42.      * Updates a given entity.
  43.      * Use the returned instance for further operations as the save operation might have changed the
  44.      * entity instance completely.
  45.      */
  46.     public <S extends T> S update(S entity) {
  47.         return em.merge(entity);
  48.     }
  49.  
  50.     /**
  51.      * Retrieves an entity by its id.
  52.      */
  53.     public T findById(ID id) {
  54.         return em.find(clazz, id);
  55.     }
  56.  
  57.     /**
  58.      * Returns all instances of the type.
  59.      */
  60.     public Iterable<T> findAll() {
  61.         CriteriaBuilder cb = em.getCriteriaBuilder();
  62.         CriteriaQuery<T> cq = cb.createQuery(clazz);
  63.         Root<T> rootEntry = cq.from(clazz);
  64.         CriteriaQuery<T> all = cq.select(rootEntry);
  65.  
  66.         TypedQuery<T> allQuery = em.createQuery(all);
  67.         return allQuery.getResultList();
  68.     }
  69.  
  70.     /**
  71.      * Deletes the entity with the given id.
  72.      */
  73.     public void deleteById(ID id) {
  74.         CriteriaBuilder cb = this.em.getCriteriaBuilder();
  75.         CriteriaDelete<T> delete = cb.createCriteriaDelete(clazz);
  76.         Root<T> e = delete.from(clazz);
  77.         // TODO JPA Metamodel
  78.         delete.where(cb.equal(e.get("id"), id));
  79.  
  80.         this.em.createQuery(delete).executeUpdate();
  81.     }
  82.  
  83.     /**
  84.      * Deletes a given entity.
  85.      */
  86.     public void delete(T entity) {
  87.         em.remove(entity);
  88.     }
  89.  
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement