tuxmartin

Generic DAO

Oct 9th, 2014
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.52 KB | None | 0 0
  1. package net.example.com.dao;
  2.  
  3. import java.util.List;
  4.  
  5. public interface GenericDao<T> {   
  6.     public T findById(int id); 
  7.     public List<T> findAll();  
  8.     public void update(T entity);  
  9.     public void save(T entity);
  10.     public void delete(T entity);
  11. }
  12.  
  13. /* ------------------------------------------------------ */
  14.  
  15. package net.example.com.dao;
  16.  
  17. import java.io.Serializable;
  18. import java.util.List;
  19.  
  20. import org.hibernate.Session;
  21. import org.hibernate.SessionFactory;
  22. import org.springframework.beans.factory.annotation.Autowired;
  23. import org.springframework.context.annotation.Scope;
  24.  
  25. @Scope("prototype")
  26. public abstract class GenericHibernateDaoImpl<T extends Serializable> implements GenericDao<T> {
  27.    
  28.     private Class<T> clazz;
  29.    
  30.     @Autowired
  31.     private SessionFactory sessionFactory;
  32.    
  33.     public final void setClazz(Class<T> clazzToSet) {
  34.         this.clazz = clazzToSet;       
  35.     }
  36.    
  37.     @SuppressWarnings("unchecked")
  38.     public T findById(int id) {
  39.         return (T) getCurrentSession().get(clazz, id);
  40.     }
  41.    
  42.     @SuppressWarnings("unchecked")
  43.     public List<T> findAll() {
  44.         return getCurrentSession().createQuery("FROM " + clazz.getName()).list();      
  45.     }
  46.    
  47.     public void update(T entity) {
  48.         getCurrentSession().update(entity);    
  49.     }
  50.    
  51.     public void save(T entity) {
  52.         getCurrentSession().save(entity);      
  53.     }
  54.    
  55.     public void delete(T entity) {
  56.         getCurrentSession().delete(entity);    
  57.     }
  58.    
  59.     protected final Session getCurrentSession(){
  60.         return sessionFactory.getCurrentSession();
  61.     }
  62. }
  63.  
  64. /* ------------------------------------------------------ */
  65.  
  66. package net.example.com.dao;
  67.  
  68. import java.util.List;
  69.  
  70. import net.example.com.entity.User;
  71.  
  72. public interface UserDao extends GenericDao<User> {
  73.  
  74.     public User findByEmail(String email);
  75.  
  76.     public List<User> findEnabled();    
  77. }
  78.  
  79. /* ------------------------------------------------------ */
  80.  
  81. package net.example.com.dao;
  82.  
  83. import java.util.List;
  84.  
  85. import org.hibernate.SessionFactory;
  86. import org.springframework.beans.factory.annotation.Autowired;
  87. import org.springframework.stereotype.Repository;
  88.  
  89. import net.example.com.entity.User;
  90.  
  91. @Repository
  92. public class UserDaoImpl extends GenericHibernateDaoImpl<User> implements UserDao {
  93.  
  94.     public User findByEmail(String email) {
  95.         return (User) getCurrentSession()
  96.                 .createQuery("FROM User WHERE email = :email")
  97.                 .setString("email", email).uniqueResult();
  98.     }
  99.  
  100.     @SuppressWarnings("unchecked")
  101.     public List<User> findEnabled() {
  102.         return getCurrentSession()
  103.                 .createQuery("FROM User WHERE enabled = true")
  104.                 .list();
  105.     }  
  106. }
Advertisement
Add Comment
Please, Sign In to add comment