Welcome 微信登录

首页 / 数据库 / MySQL / Hibernate实用类创建SessionFactory

  1. //通过Configuration的对象调用configure()方法加载Hibernate配置文件创建SessionFactory 由于SessionFactory 的创建费时,所以放在static下面   
  2. //让他只初始化一次最后返回一个SessionFactory 。   
  3.   
  4. //下面是两种模式的SessionFactory:一种是通过映射文件,一种是通过注解。   
  5. public class HibernateUtilMapping {  
  6. private static final SessionFactory sessionFactory =buildSessionFactory();  
  7.   
  8. private static SessionFactory buildSessionFactory(){  
  9. try {  
  10. return new Configuration().configure().buildSessionFactory();  
  11. catch (Throwable ex) {  
  12. // TODO Auto-generated catch block   
  13. System.err.println("Initial SessionFactory creationfailed."+ex);  
  14. throw new ExceptionInInitializerEr<div><wbr>ror(ex);  
  15. }  
  16. }  
  17.   
  18. public static SessionFactory getSessionFactory(){  
  19. return sessionFactory;  
  20. }  
  21.   
  22. public static void closeSession(Session session){  
  23. if(session.isOpen()){  
  24. session.close();  
  25. }  
  26. }  
  27. }  
  28.   
  29.   
  30. public class HibernateUtilAnnotation {  
  31.   
  32. private static final SessionFactory sessionFactory =buildSessionFactory();  
  33.   
  34. private static SessionFactory buildSessionFactory(){  
  35. try {  
  36. return newAnnotationConfiguration().configure().buildSessionFactory();  
  37. catch (Throwable ex) {  
  38. // TODO Auto-generated catch block   
  39. System.err.println("Initial SessionFactory creationfailed."+ex);  
  40. throw new ExceptionInInitializerEr<wbr>ror(ex);  
  41. }  
  42. }  
  43.   
  44. public static SessionFactory getSessionFactory(){  
  45. return sessionFactory;  
  46. }  
  47.   
  48. public static void closeSession(Session session){  
  49. if(session.isOpen()){  
  50. session.close();  
  51. }  
  52. }  
  53. }
Hibernate分页小结Hibernate-类映射文件中常用属性的了解相关资讯      hibernate 
  • Hibernate利用@DynamicInsert和@  (今 07:09)
  • Hibernate某些版本(4.3)下报错   (04月20日)
  • Hibernate 5.1.0 正式版发布下载  (02月12日)
  • Hibernate的get和load的区别  (08月07日)
  • Hibernate3.1.2_中文文档PDF  (02月17日)
  • Hibernate ORM 5.0.6 发布下载  (12/17/2015 17:12:55)
本文评论 查看全部评论 (0)
表情: 姓名: 字数