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的操作很多都是类似的,下面是我举出的一 ...
随机推荐
- 关于QT的QPainterPath::arcTo 详解
这个函数文档的意思就是画弧,看了文档也不太明白,自己做了demo终于明白了意思 移动到圆心,画180度半圆 void TestArcTo::paintEvent(QPaintEvent *) { QP ...
- CMake Error: CMake was unable to find a build program corresponding to "Ninja".
系统环境: $ lsb_release -a LSB Version: :base-4.0-amd64:base-4.0-noarch:core-4.0-amd64:core-4.0-noarch:g ...
- xinwenti
angularjs angular2脏检查机制和数据双向绑定远离 angular2 aot编译
- php 获取自己的公网IP
<?php $externalContent = file_get_contents('http://checkip.dyndns.com/'); preg_match('/Current IP ...
- 『PyTorch』第十三弹_torch.nn.init参数初始化
初始化参数的方法 nn.Module模块对于参数进行了内置的较为合理的初始化方式,当我们使用nn.Parameter时,初始化就很重要,而且我们也可以指定代替内置初始化的方式对nn.Module模块进 ...
- python-day43--多表查询
一.多表连接查询: #重点:外链接语法 准备表 #建表 create table department( id int, name varchar(20) ); create table ...
- CoderForce 148D-Bag of mice (概率DP求概率)
题目大意:美女与野兽在玩画鸽子的游戏.鸽子在用黑布遮住的笼子里,白色的有w只,黑色的有b只,每次拿出一只作画,谁先画到白色的鸽子谁就赢.美女首先画,因为野兽太丑,它每次画的时候都会吓跑一只鸽子,所有出 ...
- xhost + 的作用
xhost 是用来控制X server访问权限的. 通常当你从hostA登陆到hostB上运行hostB上的应用程序时, 做为应用程序来说,hostA是client,但是作为图形来说, 是在hostA ...
- 使用yum安装php72
1.执行yum search php72w查看自己的yum源有没有php72 如果自己有安装其它php版本先执行yum -y remove php* 2.没有php72执行以下命令扩展yum库,以下命 ...
- 对mysql性能影响的几个重要参数---高性能(七)
转载地址:https://wenku.baidu.com/view/f6bd760cb307e87101f696e3.html