/**
* Location of hibernate.cfg.xml file.
* Location should be on the classpath as Hibernate uses
* #resourceAsStream style lookup for its configuration file.
* The default classpath location of the hibernate config file is
* in the default package. Use #setConfigFile() to update
* the location of the configuration file for the current session.
*/
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
  private static Configuration configuration = new AnnotationConfiguration();
private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;
private static String configFile = CONFIG_FILE_LOCATION; static {
try {
/*
* Hibernate4.3 已经放弃用该方式注册sessionFactory,
* 可以用如下方式获取。
*/
configuration.configure(configFile);
serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry); } catch (Exception e) {
System.err.println("##############################Error Creating SessionFactory##############################");
e.printStackTrace();
}
}
private HibernateSessionFactory() {
} /**
* Returns the ThreadLocal Session instance. Lazy initialize
* the <code>SessionFactory</code> if needed.
*
* @return Session
* @throws HibernateException
*/  public static Session getSession() throws HibernateException {
Session session = (Session) threadLocal.get();
if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
rebuildSessionFactory();
}
session = (sessionFactory != null) ? sessionFactory.openSession() : null;
threadLocal.set(session);
}
return session;
}

Hibernate4获取sessionFactory的更多相关文章

  1. Hibernate4 获取SessionFactory

    import org.hibernate.HibernateException; import org.hibernate.SessionFactory; import org.hibernate.c ...

  2. 不同版本Hibernate.获取SessionFactory的方式

    不同版本Hibernate.获取SessionFactory的方式 Hibernate 版本说明: 我当前使用的是 Hibernate 5.x ,(hibernate-release-5.3.6.Fi ...

  3. hibernate不同版本获取获取sessionFactory

    hibernate4时,我们采用以下方式获取会话工厂: // 1. 解析我们在hibernate.cfg.xml中的配置 Configuration configuration = new Confi ...

  4. Hibernate4获取Connection,ResultSet对象

    项目中需要一个json对象,封装的时候,需要数据的列名. 在jdbc里面,可以有个ResultMetaData对象获取列名字.因为我用的是hibernate,这个框架已经封装了很多,一般是难以获得re ...

  5. spring整合hibernate,在获取sessionFactory的时候报错,求解决办法!!

    applicationContext.xml文件 <!-- 开启扫包 --> <context:component-scan base-package="cn.edu&qu ...

  6. 让我的分页类获取sessionFactory

    我们知道在Hibernate里比较重要的sessionFactory,经过Spring的管理可以很好地为Spring里注入使用的bean服务(提供数据源的使用),但是,当我们所要使用的类不是像我们尝试 ...

  7. 4.0之后的hibernate获取sessionFactory

    static{ Configuration config=new Configuration().configure(); ServiceRegistry resgistry = new Servic ...

  8. hibernate4.0中SessionFactory的创建

    创建SessionFactory 首先创建Configuration对象,主要方式是: new Configuration().configure() 默认情况下Hibernate会去classPat ...

  9. 基于Spring4+Hibernate4的通用数据访问层+业务逻辑层(Dao层+Service层)设计与实现!

    基于泛型的依赖注入.当我们的项目中有很多的Model时,相应的Dao(DaoImpl),Service(ServiceImpl)也会增多. 而我们对这些Model的操作很多都是类似的,下面是我举出的一 ...

随机推荐

  1. RabbitMQ入门_05_多线程消费同一队列

    A. 多线程消费同一队列 参考资料:https://www.rabbitmq.com/tutorials/tutorial-two-java.html 消费一条消息往往比产生一条消息慢很多,为了防止消 ...

  2. threejs和particles.js

    particles.js 在线配置的东西: http://vincentgarreau.com/particles.js/ https://threejs.org/ http://thehuub.co ...

  3. 2017-03-03 Oracle在.Net中出现未在本地计算机上注册“OraOLEDB.Oracle”提供程序的错误

    在前面的Oracle配置完成后,打开项目运行出错,出现未在本地计算机上注册“OraOLEDB.Oracle”提供程序的错误,看到“注册”两个字,首先想到,难道还要用命令行注册一下?果不其然,需要手动注 ...

  4. 49 BOM 和DOM

    一.BOM window 对象  1.确认,输入,    window.alert(123) // 弹框    let ret = window.confirm("是否删除") / ...

  5. Java GUI编程中AWT/swing/SWT的优缺点

    http://www.cnblogs.com/dugang/archive/2010/10/22/1858478.html AWT AWT是Abstract Window Toolkit(抽象窗口工具 ...

  6. consumer的DubboClientHandler线程池

    1. 创建线程池 创建线程池的调用栈如下: SimpleDataStore把线程池存放在map中. public class NettyClient extends AbstractClient { ...

  7. intel python加速效果初探

    python3安装intel的加速库: conda config --add channels intel conda create --name intelpy intelpython3_full ...

  8. java并发编程:线程安全管理类--原子操作类--AtomicLongArray

    1.类 AtomicLongArray public class AtomicLongArray extends Object implements Serializable 可以用原子方式更新其元素 ...

  9. currentSession

    public  class  HibernateUtil  {  public static final ThreadLocal session =new ThreadLocal();  public ...

  10. FZU 2128 最长子串

    题目链接:最长子串 思路:依次找出每个子串的在字符串中的首尾地址,所有子串先按照尾地址从小到大排序.然后首地址从小到大排. 遍历一遍每个子串的首地址和它后面相邻子串的尾地址之差-1, 第一个子串的首地 ...