你好,游客 登录 注册 搜索
背景:
阅读新闻

Hibernate的HQL查询和工具类的增强

[日期:2012-02-29] 来源:Linux社区  作者:a352193394 [字体:大 中 小]

其实对Hibernate进行分析后可以发现,这个Hibernate对于对象的增加。删除,更新是差不多的,所以可以把他们整合到一个工具类中去。还有就是,因为查询是很复杂的,所以对于数据库的查询,就有相对应的接口,类似于JDBC中的PreparedStatemented这个类。下面我们先看一下工具类的增强

  1. package com.bird.hibernate.test;  
  2.   
  3. import org.hibernate.HibernateException;  
  4. import org.hibernate.Session;  
  5. import org.hibernate.SessionFactory;  
  6. import org.hibernate.Transaction;  
  7. import org.hibernate.cfg.Configuration;  
  8.   
  9. @SuppressWarnings("deprecation")  
  10. public final class Hibernateutils {  
  11.       
  12.     private static SessionFactory sf;  
  13.       
  14.     private Hibernateutils(){}  
  15.       
  16.     static{  
  17.         Configuration cfg = new Configuration();  
  18.         cfg.configure();  
  19.         sf = cfg.buildSessionFactory();  
  20.     }  
  21.   
  22.     public static SessionFactory getSessionFactory() {  
  23.         return sf;  
  24.     }  
  25.       
  26.     public static Session getSession(){  
  27.         return sf.openSession();  
  28.     }  
  29.       
  30.     public static void add(Object entity){  
  31.         Session s = null;  
  32.         Transaction ts = null;  
  33.   
  34.         try {  
  35.             s = sf.openSession();  
  36.             ts = s.beginTransaction();  
  37.             s.save(entity);  
  38.             ts.commit();  
  39.         } catch (HibernateException e) {  
  40.             if (ts != null)  
  41.                 ts.rollback();  
  42.             throw e;  
  43.         } finally {  
  44.             if (s != null)  
  45.                 s.close();  
  46.         }  
  47.     }  
  48.       
  49.     public static void update(Object entity){  
  50.         Session s = null;  
  51.         Transaction ts = null;  
  52.   
  53.         try {  
  54.             s = sf.openSession();  
  55.             ts = s.beginTransaction();  
  56.             s.update(entity);  
  57.             ts.commit();  
  58.         } catch (HibernateException e) {  
  59.             if (ts != null)  
  60.                 ts.rollback();  
  61.             throw e;  
  62.         } finally {  
  63.             if (s != null)  
  64.                 s.close();  
  65.         }  
  66.     }  
  67.       
  68.       
  69.     public static void delete(Object entity){  
  70.         Session s = null;  
  71.         Transaction ts = null;  
  72.   
  73.         try {  
  74.             s = sf.openSession();  
  75.             ts = s.beginTransaction();  
  76.             s.delete(entity);  
  77.             ts.commit();  
  78.         } catch (HibernateException e) {  
  79.             if (ts != null)  
  80.                 ts.rollback();  
  81.             throw e;  
  82.         } finally {  
  83.             if (s != null)  
  84.                 s.close();  
  85.         }  
  86.     }  
  87. }  
linux
相关资讯       hibernate 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款