/**
* 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. Android系统广播处理机制

    Android系统中的广播是广泛用于应用程序之间通信的一种手段,它类似于事件处理机制,不同的地方就是广播的处理是系统级别的事件处理过程(一般事件处理是控件级别的).在此过程中仍然是离不开Intent对 ...

  2. Codeforces 534B - Covered Path

    534B - Covered Path 思路:贪心,每一秒取尽可能大并且可以达到的速度. 画张图吧,不解释了: 代码: #include<bits/stdc++.h> using name ...

  3. Java基础九--抽象类

    Java基础九--抽象类 一.抽象类介绍 /*抽象类:抽象:笼统,模糊,看不懂!不具体. 特点:1,方法只有声明没有实现时,该方法就是抽象方法,需要被abstract修饰. 抽象方法必须定义在抽象类中 ...

  4. English trip -- VC(情景课)10 A Get ready 预备课

    Words dance  跳舞 exercise  运动:锻炼 fish  鱼 play basketball  打篮球 play cards 玩牌 swim  游泳 decorations 装饰品 ...

  5. Android出现declaration of org.apache.http.message.BasicLineFormatter appears in /system/framework/

    这是由于使用了CloseableHttpClient造成的,把 CloseableHttpClient httpclient = HttpClients.createDefault(); Closea ...

  6. 【转】Vue.js特点

    作为一名Vue.js的忠实用户,我想有必要写点文章来歌颂这一门美好的语言了,我给它的总体评价是“简单却不失优雅,小巧而不乏大匠”,下面将围绕这句话给大家介绍Vue.js,希望能够激发你对Vue.js的 ...

  7. Ubuntu 18.04 LTS 安装wine 、exe程序安装和卸载

    什么是wine?Wine(是“Wine Is Not an Emulator”的缩写)是一个兼容层,能够在几个POSIX兼容的操作系统上运行Windows应用程序,如Linux.MaOS.BSD.代替 ...

  8. hdu3068 manacher模板题

    给出一个只由小写英文字符a,b,c...y,z组成的字符串S,求S中最长回文串的长度. 回文就是正反读都是一样的字符串,如aba, abba等 Input输入有多组case,不超过120组,每组输入为 ...

  9. 使用XML Publisher导出PDF报表

    生成XML数据源有两种方式. 一种是使用存储过程,返回一个clob作为xml数据源. 另一种是直接使用VO中的数据生成xml数据源. 方法一参考: Oracle XML Publisher技巧集锦 O ...

  10. 55. 45. Jump Game II *HARD*

    1. Given an array of non-negative integers, you are initially positioned at the first index of the a ...