hibernate使用SessionFactory的时候用单例和JUnit里的创建方法
来源:互联网 发布:linux怎么防御勒索 编辑:程序博客网 时间:2024/05/21 11:05
因为创建SessionFactory会很慢,建一次就行,用static 创建单例即可,以后用到了直接调用即可
package util;import org.hibernate.*;import org.hibernate.cfg.*;public class HibernateUtil { private static final SessionFactory sessionFactory; static { try { // Create the SessionFactory from hibernate.cfg.xml sessionFactory = new Configuration().configure().buildSessionFactory(); } catch (Throwable ex) { // Make sure you log the exception, as it might be swallowed System.err.println("Initial SessionFactory creation failed." + ex); throw new ExceptionInInitializerError(ex); } } public static SessionFactory getSessionFactory() { return sessionFactory; }}
在JUnit中在类里可以这么写
public static SessionFactory sf = null;
@BeforeClasspublic static void beforeClass(){sf = new AnnotationConfiguration().configue().buildSessionFactory();@Test....类的其他内容@AfterClasspublic static void afterClass(){sf.close();}}
- hibernate使用SessionFactory的时候用单例和JUnit里的创建方法
- Junit4 bug 使用hibernate 创建sessionfactory的时候
- Hibernate下搭建JUNIT的测试环境,使用beforeclass和afterclass实现sessionFactory建立一次
- Hibernate里sessionFactory的openSession()和getCurrentSession()的区别
- Hibernate 中 SessionFactory的创建
- hibernate中SessionFactory的创建
- hibernate 创建 SessionFactory 的变化
- Hibernate SessionFactory 的创建方式
- Spring创建Hibernate的SessionFactory的简易方法
- hibernate SessionFactory的获取方法
- hibernate 获取SessionFactory的方法
- Hibernate中SessionFactory的创建和Session的获取
- Junit 使用中,关于SessionFactory的建立
- Hibernate创建sessionFactory,session和关闭session的工具类
- hibernate的sessionFactory和session
- Hibernate的SessionFactory和Session
- Hibernate SessionFactory中openSession和getCurrentSession方法的区别
- Hibernate SessionFactory中openSession和getCurrentSession方法的区别
- 腾讯 vs 百度
- Creating and/or Removing a Login Item
- 单片机秒表计时器
- Nginx应用案例分享:压力测试
- 当前标识(NT AUTHORITY\NETWORK SERVICE)没有对“D:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NE
- hibernate使用SessionFactory的时候用单例和JUnit里的创建方法
- OOA、OOD、OOP
- Java Socket网络编程初级入门
- zf 视图 $this->view
- 关于solr不能搜索中文
- Boost智能指针——scoped_ptr和shared_ptr
- Opening System Preferences Pane
- Vector向量几种情况的总结
- .NET 发送邮件