tuxmartin

Generic DAO & Service

Oct 9th, 2014
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.34 KB | None | 0 0
  1. /* ################################# DAO ################################ */
  2. package net.example.com.dao;
  3.  
  4. import java.util.List;
  5.  
  6. public interface GenericDao<T> {   
  7.     public T findById(int id); 
  8.     public List<T> findAll();  
  9.     public void update(T entity);  
  10.     public void save(T entity);
  11.     public void delete(T entity);
  12. }
  13.  
  14. /* ------------------------------------------------------ */
  15.  
  16. package net.example.com.dao;
  17.  
  18. import java.io.Serializable;
  19. import java.util.List;
  20.  
  21. import org.hibernate.Session;
  22. import org.hibernate.SessionFactory;
  23. import org.springframework.beans.factory.annotation.Autowired;
  24. import org.springframework.context.annotation.Scope;
  25.  
  26. @Scope("prototype")
  27. public abstract class GenericHibernateDaoImpl<T extends Serializable> implements GenericDao<T> {
  28.    
  29.     private Class<T> clazz;
  30.    
  31.     @Autowired
  32.     private SessionFactory sessionFactory;
  33.    
  34.     public final void setClazz(Class<T> clazzToSet) {
  35.         this.clazz = clazzToSet;       
  36.     }
  37.    
  38.     @SuppressWarnings("unchecked")
  39.     public T findById(int id) {
  40.         return (T) getCurrentSession().get(clazz, id);
  41.     }
  42.    
  43.     @SuppressWarnings("unchecked")
  44.     public List<T> findAll() {
  45.         return getCurrentSession().createQuery("FROM " + clazz.getName()).list();      
  46.     }
  47.    
  48.     public void update(T entity) {
  49.         getCurrentSession().update(entity);    
  50.     }
  51.    
  52.     public void save(T entity) {
  53.         getCurrentSession().save(entity);      
  54.     }
  55.    
  56.     public void delete(T entity) {
  57.         getCurrentSession().delete(entity);    
  58.     }
  59.    
  60.     protected final Session getCurrentSession(){
  61.         return sessionFactory.getCurrentSession();
  62.     }
  63. }
  64.  
  65. /* ------------------------------------------------------ */
  66.  
  67. package net.example.com.dao;
  68.  
  69. import net.example.com.entity.Country;
  70.  
  71. public interface CountryDao extends GenericDao<Country> {
  72.  
  73.     public Country findByName(String name);    
  74.     public Country findByCode(String code);
  75.  
  76. }
  77.  
  78. /* ------------------------------------------------------ */
  79.  
  80. package net.example.com.dao;
  81.  
  82. import org.springframework.stereotype.Repository;
  83.  
  84. import net.example.com.entity.Country;
  85.  
  86. @Repository
  87. public class CountryDaoImpl extends GenericHibernateDaoImpl<Country> implements CountryDao {
  88.  
  89.     @Override
  90.     public Country findByName(String name) {
  91.         return (Country) getCurrentSession()
  92.                 .createQuery("FROM Country WHERE name = :name")
  93.                 .setString("name", name).uniqueResult();
  94.     }
  95.  
  96.     @Override
  97.     public Country findByCode(String code) {
  98.         return (Country) getCurrentSession()
  99.                 .createQuery("FROM Country WHERE code = :code")
  100.                 .setString("code", code).uniqueResult();
  101.     }
  102.  
  103. }
  104.  
  105. /* ################################# DAO ################################ */
  106.  
  107.  
  108.  
  109. /* ################################# SERVICE ################################ */
  110.  
  111. package net.example.com.service;
  112.  
  113. import java.util.List;
  114.  
  115. public interface GenericManager<T> { // GenericManager<T> = GenericDao<T>
  116.    
  117.     public T findById(int id); 
  118.     public List<T> findAll();  
  119.     public void update(T entity);  
  120.     public void save(T entity);
  121.     public void delete(T entity);
  122. }
  123.  
  124. /* ------------------------------------------------------ */
  125.  
  126. package net.example.com.service;
  127.  
  128. import java.util.List;
  129.  
  130. import org.springframework.beans.factory.annotation.Autowired;
  131. import org.springframework.stereotype.Service;
  132.  
  133. import net.example.com.dao.GenericDao;
  134.  
  135. @Service
  136. public abstract class GenericManagerImpl<T> implements GenericManager<T> {
  137.  
  138.     @Autowired
  139.     protected GenericDao<T> dao;
  140.  
  141.     @Override
  142.     public T findById(int id) {
  143.         return dao.findById(id);
  144.     }
  145.  
  146.     @Override
  147.     public List<T> findAll() {
  148.         return dao.findAll();
  149.     }
  150.  
  151.     @Override
  152.     public void update(T entity) {
  153.         dao.update(entity);
  154.     }
  155.  
  156.     @Override
  157.     public void save(T entity) {
  158.         dao.save(entity);
  159.     }
  160.  
  161.     @Override
  162.     public void delete(T entity) {
  163.         dao.delete(entity);
  164.     }
  165. }
  166. /* ------------------------------------------------------ */
  167.  
  168. package net.example.com.dao;
  169.  
  170. import net.example.com.entity.Country;
  171.  
  172. public interface CountryManager extends GenericDao<Country> { // CountryManager = CountryDao
  173.  
  174.     public Country findByName(String name);    
  175.     public Country findByCode(String code);
  176. }
  177.  
  178. /* ------------------------------------------------------ */
  179.  
  180. package net.example.com.service;
  181.  
  182. import java.util.List;
  183.  
  184. import javax.transaction.Transactional;
  185.  
  186. import org.springframework.beans.factory.annotation.Autowired;
  187. import org.springframework.stereotype.Service;
  188.  
  189. import net.example.com.dao.CountryDao;
  190. import net.example.com.entity.Country;
  191.  
  192. @Service
  193. @Transactional
  194. public class CountryManagerImpl extends GenericManagerImpl<Country> implements CountryManager {
  195.  
  196.     @Override
  197.     public List<Country> findAll() {
  198.         return dao.findAll();
  199.     }
  200.  
  201.     public Country findById(int id) {
  202.         return dao.findById(id);
  203.     }
  204.  
  205.     @Override
  206.     public Country findByName(String name) {
  207.         return dao.findByName(name); // nevidi to metodu findByName !!!!!!!!!
  208.     }
  209.  
  210.     @Override
  211.     public Country findByCode(String code) {
  212.         return dao.findByCode(code); // nevidi to metodu findByCode !!!!!!!!!
  213.     }
  214.  
  215.     @Override
  216.     public void save(Country country) {
  217.         dao.save(country);
  218.     }
  219.  
  220.     @Override
  221.     public void delete(Country country) {
  222.         dao.delete(country);
  223.     }
  224.  
  225.     @Override
  226.     public void update(Country country) {
  227.         dao.update(country);
  228.     }
  229.  
  230. }
  231.  
  232. /* ------------------------------------------------------ */
  233.  
  234. /* ################################# SERVICE ################################ */
Advertisement
Add Comment
Please, Sign In to add comment