Hibernate4获取sessionFactory
/**
* 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的更多相关文章
- Hibernate4 获取SessionFactory
import org.hibernate.HibernateException; import org.hibernate.SessionFactory; import org.hibernate.c ...
- 不同版本Hibernate.获取SessionFactory的方式
不同版本Hibernate.获取SessionFactory的方式 Hibernate 版本说明: 我当前使用的是 Hibernate 5.x ,(hibernate-release-5.3.6.Fi ...
- hibernate不同版本获取获取sessionFactory
hibernate4时,我们采用以下方式获取会话工厂: // 1. 解析我们在hibernate.cfg.xml中的配置 Configuration configuration = new Confi ...
- Hibernate4获取Connection,ResultSet对象
项目中需要一个json对象,封装的时候,需要数据的列名. 在jdbc里面,可以有个ResultMetaData对象获取列名字.因为我用的是hibernate,这个框架已经封装了很多,一般是难以获得re ...
- spring整合hibernate,在获取sessionFactory的时候报错,求解决办法!!
applicationContext.xml文件 <!-- 开启扫包 --> <context:component-scan base-package="cn.edu&qu ...
- 让我的分页类获取sessionFactory
我们知道在Hibernate里比较重要的sessionFactory,经过Spring的管理可以很好地为Spring里注入使用的bean服务(提供数据源的使用),但是,当我们所要使用的类不是像我们尝试 ...
- 4.0之后的hibernate获取sessionFactory
static{ Configuration config=new Configuration().configure(); ServiceRegistry resgistry = new Servic ...
- hibernate4.0中SessionFactory的创建
创建SessionFactory 首先创建Configuration对象,主要方式是: new Configuration().configure() 默认情况下Hibernate会去classPat ...
- 基于Spring4+Hibernate4的通用数据访问层+业务逻辑层(Dao层+Service层)设计与实现!
基于泛型的依赖注入.当我们的项目中有很多的Model时,相应的Dao(DaoImpl),Service(ServiceImpl)也会增多. 而我们对这些Model的操作很多都是类似的,下面是我举出的一 ...
随机推荐
- 在每个节点填充向右的指针 Populating Next Right Pointers in Each Node
2018-08-09 16:01:40 一.Populating Next Right Pointers in Each Node 问题描述: 问题求解: 由于是满二叉树,所以很好填充. public ...
- GitHub 中国区前 100 名到底是什么样的人?
转一下CSDN的文章, 这里有些人挺厉害的. http://geek.csdn.net/news/detail/66000
- springboot 解决 The bean 'userRepository', defined in null, could not be registered. A bean with that name has already been defined in file XXX and overriding is disabled.
1.springboot 启动时报错: 2019-02-20 14:59:58.226 INFO 10092 --- [ main] c.f.s.SpringbootssmApplication : ...
- 真核生物基因结构 & mRNA结构
参考: 分子生物学教材 再一次,翻看真核生物基因结构! mRNA基本结构特点 Structure and function of Messenger RNA (mRNA ) 基因结构 其实这个结构不完 ...
- 3.4 复杂的x86指令举例
计算机组成 3 指令系统体系结构 3.4 复杂的x86指令举例 x86作为复杂指令系统的代表,自然会有不少相当复杂的指令.在这一节我们将会看到其中有代表性的一些例子. 关于复杂的x86指令,我们这里举 ...
- python-day45--mysql索引
一 .介绍 为何要有索引? 一些复杂的查询操作,对查询语句的优化显然是重中之重.说起加速查询,就不得不提到索引了. 什么是索引? 索引在MySQL中也叫做“键”,是存储引擎用于快速找到记录的一种数据结 ...
- ubuntu vim简单命令
1.ubuntu vim 一些基本的命令. :set nu 或着 set number 设置行数 :set nonu 取消行数 ctrl+u 将 ...
- Leetcode 89
回溯写到自闭:不想就删了: class Solution { public: vector<int> grayCode(int n) { vector<vector<int&g ...
- 在菜鸟教程学 HTML(一)
注意:对于中文网页需要使用 <meta charset="utf-8"> 声明编码,否则会出现乱码.有些浏览器会设置 GBK 为默认编码,则你需要设置为 <met ...
- dns 服务架构优化 - 百万级并发不是梦 - bind+namedmanager+dnsmasq
bind: DNS服务端. namedmanager: DNS web管理页面. dnsmasq: 并发查询上游dns域名解析. 问题:作为消息推送业务,单台业务机器域名解析并发达到上万次.业务机器集 ...