Hibernate初学者,不加SessionFactory,自己写

BaseHibernate

 1 package com.proj.dao;
 2 
 3 import java.io.Serializable;
 4 
 5 import org.hibernate.HibernateException;
 6 import org.hibernate.Session;
 7 import org.hibernate.SessionFactory;
 8 import org.hibernate.Transaction;
 9 import org.hibernate.cfg.Configuration;
10 
11 public class BaseHibernateDao {
12 
13     protected SessionFactory sf ;
14     protected Session session;
15     
16     public SessionFactory getSessionFactory(){
17         Configuration cfg = new Configuration().configure();
18         return cfg.buildSessionFactory();
19     }
20     public void getSession(){
21          sf = this.getSessionFactory();
22          session = sf.openSession();
23     }
24     public Serializable add(Object obj){
25         Serializable id = null;
26         getSession();
27         Transaction tx= null;
28         try {
29             tx = session.beginTransaction();
30             id= session.save(obj);
31             tx.commit();
32         } catch (HibernateException e) {
33             if(tx!=null){
34                 tx.rollback();
35             }
36             e.printStackTrace();
37         }finally{
38             
39             session.close();
40         }
41         return id;
42     }
43     public void update(Object obj){
44         getSession();
45         Transaction tx= null;
46         try {
47             tx = session.beginTransaction();
48             session.update(obj);
49             tx.commit();
50         } catch (HibernateException e) {
51             if(tx!=null){
52                 tx.rollback();
53             }
54             e.printStackTrace();
55         }finally{
56             
57             session.close();
58         }
59     }
60     public Object get(Class clazz,Serializable id){
61         Object obj = null;
62         this.getSession();
63         
64         try {
65             obj = session.get(clazz, id);
66         } catch (HibernateException e) {
67             // TODO Auto-generated catch block
68             e.printStackTrace();
69         }finally{
70             session.close();
71         }
72         
73         return obj;
74     }
75     public void delete(Class clazz,Serializable id){
76         Object obj = null;
77         this.getSession();
78         obj = session.get(clazz, id);
79         Transaction tx= null;
80         try {
81             tx = session.beginTransaction();
82             session.delete(obj);
83             tx.commit();
84         } catch (HibernateException e) {
85             if(tx!=null){
86                 tx.rollback();
87             }
88             e.printStackTrace();
89         }finally{
90             session.close();
91         }
92     }
93 }

Dao类

 1 package com.proj.dao;
 2 
 3 import java.util.List;
 4 
 5 import org.hibernate.Query;
 6 import org.hibernate.Session;
 7 import org.hibernate.SessionFactory;
 8 import org.hibernate.Transaction;
 9 import org.hibernate.cfg.Configuration;
10 
11 import com.proj.entity.Stu;
12 
13 public class StuDao extends BaseHibernateDao {
14 
15     public List<Stu> getAll() {
16         Configuration cfg = new Configuration().configure();
17         SessionFactory sf = cfg.buildSessionFactory();
18         Session session = sf.openSession();
19         String hql = "from Stu";
20         Query query = session.createQuery(hql);
21         session.close();
22         return query.list();
23     }
24     public Stu getById(String studentId){
25         return (Stu)super.get(Stu.class, studentId);    
26     }
27     public String addStu(Stu stu){
28         return (String)super.add(stu);
29     }
30     public void updateStu(Stu stu){
31         super.update(stu);
32     }
33     public void deleteStu(String studentId){
34         super.delete(Stu.class, studentId);
35     }
36 }

 

posted @ 2012-09-10 11:31  小伙、不一般  阅读(1150)  评论(0编辑  收藏  举报