Advertisement
zubayer007

Generic DAO Using Hibernate

Sep 26th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.29 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package ac.dia.dao;
  7.  
  8. import ac.dia.util.HibernateUtil;
  9. import java.io.Serializable;
  10. import java.util.List;
  11. import org.hibernate.Session;
  12. import org.hibernate.Transaction;
  13.  
  14. /**
  15.  *
  16.  * @author user1
  17.  * @param <T>
  18.  * @param <ID>
  19.  */
  20. public abstract class GenaricDAO<T, ID extends Serializable> {
  21.  
  22.     Session session = HibernateUtil.getSessionFactory().openSession();
  23.     T obj;
  24.  
  25.     public void saveData(T obj) throws Exception {
  26.         Transaction t = session.beginTransaction();
  27.         session.save(obj);
  28.         t.commit();
  29.     }
  30.  
  31.     public void updateData(T obj) throws Exception {
  32.         Transaction t = session.beginTransaction();
  33.         session.update(obj);
  34.         t.commit();
  35.     }
  36.  
  37.     public void deleteData(T obj) throws Exception {
  38.         Transaction t = session.beginTransaction();
  39.         session.delete(obj);
  40.         t.commit();
  41.     }
  42.  
  43.     public T findData(Class c, ID id) throws Exception {
  44.         return (T) session.get(c, id);
  45.     }
  46.  
  47.     public List<T> getAllData(Class c) throws Exception {
  48.         return (List<T>) session.createCriteria(c).list();
  49.     }
  50.  
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement