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的操作很多都是类似的,下面是我举出的一 ...
随机推荐
- spring boot: spring-data-jpa (Repository/CrudRepository) 数据库操作, @Entity实体类持久化
SpringBoot实现的JPA封装了JPA的特性, Repository是封装了jpa的特性(我是这么理解的) 1在pom.xml引入mysql, spring-data-jpa依赖 2.在src/ ...
- Yii 语言设置 中文提示信息
1. 在main.php配置文件中加入 'language'=>'zh_cn', 注: 在URL中追加参数lang=zh_cn即可实现中文 2. 在Controller方法中添加 publi ...
- Vue.js Client-Side Storage;( Web Storage/localStorage)
原文:https://cn.vuejs.org/v2/cookbook/client-side-storage.html LocalStorage (api) my code pen :https:/ ...
- memcached客户端连接建立过程笔记
memcached在启动过程初始化server_sockets时,根据启动参数决定系统是进行tcp监听还是udp监听,这里暂时只关注tcp的情况. server_socket在初始化时会向系统申请监听 ...
- Many Easy Problem
转自hwk0518,不胜感谢,侵删.
- LTE时代的定位技术:OTDOA,LPP,SUPL2.0
LTE时代的定位技术:OTDOA,LPP,SUPL2.0 移动定位技术的发展历程 如今智能手机已经在整个社会普及,数量众多的手机应用成为了人们生活当中不可或缺的一部分.越来越多的手机应用都用到了手机定 ...
- Population Size CodeForces - 416D (贪心,模拟)
大意: 给定$n$元素序列$a$, 求将$a$划分为连续的等差数列, 且划分数尽量小. $a$中的$-1$表示可以替换为任意正整数, 等差数列中必须也都是正整数. 贪心策略就是从前到后尽量添进一个等差 ...
- UVA-10026 Shoemaker's Problem (贪心)
题目大意:一个鞋匠,有n只鞋要修,修某只鞋的时间ti已知,某只鞋晚修一天要交的罚款fi也已知.现在让找个修鞋顺序使得罚款最少. 题目分析:本来想水一下这道题,没想到真的AC啦.后来又查的题解,找的解释 ...
- 通过一个uri获取一个Bitmap对象
Android 开发过程中,可能会用到的,通过一个uri获取一个Bitmap对象 private Bitmap getBitmapFromUri(Uri uri){ try { // 读取ur ...
- n阶汉诺塔 记住吧。。
#include "bits/stdc++.h" using namespace std; int c; void move(char a,int n,char b) { prin ...