08-session详解
如何获取session对象?
1,openSession
2,getCurrentSession
如果使用getCurrentSession需要在hibernate.cfg.xml文件中进行配置:
如果是本地事务(JDBC事务)
<property="hibernate.current_session_context_class">thread</property>
如果是全局事务(jta事务)
<property="hibernate.current_session_context_class">jta</property>
测试代码:
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.junit.Test; public class SessionTest {
@Test
public void testOpenSession()
{
Configuration config=new Configuration().configure();//获得配置对象
config.addClass(Students.class);
//获得服务注册对象。
ServiceRegistry serviceRegistry=new StandardServiceRegistryBuilder()
.applySettings(config.getProperties()).build(); //通过config.getProperties()读取配置文档。
//创建会话工厂对象
SessionFactory sessionFactory=config.buildSessionFactory(serviceRegistry);
Session session=sessionFactory.openSession();//创建会话对象
if(session!=null)
{
System.out.println("session创建成功");
}
else
{
System.out.println("创建失败");
} }
@Test
public void testGetCurrentSession()
{
Configuration config=new Configuration().configure();//获得配置对象
config.addClass(Students.class);
//获得服务注册对象。
ServiceRegistry serviceRegistry=new StandardServiceRegistryBuilder()
.applySettings(config.getProperties()).build(); //通过config.getProperties()读取配置文档。
//创建会话工厂对象
SessionFactory sessionFactory=config.buildSessionFactory(serviceRegistry);
Session session=sessionFactory.getCurrentSession();//创建会话对象
if(session!=null)
{
System.out.println("session创建成功");
}
else
{
System.out.println("创建失败");
}
}
}
运行后发现:一个运行成功,而另一个失败,是因为没有添加配置。
加上如下后,创建成功:
二者区别:
openSession与getCurrentSession区别
1,getCurrentSession在事务提交或者回滚后会自动关闭,而opensession需要手动关闭。如果使用openssesion而没有手动关闭,
多次之后会导致连接池溢出。
2,openSession每次创建使用新的session对象,getcurrentSession使用现有的session对象(类似单例模式)。
验证getCurrentSession每次重新创建对象:
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.junit.Test; public class SessionTest {
@Test
public void testOpenSession()
{
Configuration config=new Configuration().configure();//获得配置对象
config.addClass(Students.class);
//获得服务注册对象。
ServiceRegistry serviceRegistry=new StandardServiceRegistryBuilder()
.applySettings(config.getProperties()).build(); //通过config.getProperties()读取配置文档。
//创建会话工厂对象
SessionFactory sessionFactory=config.buildSessionFactory(serviceRegistry);
Session session1=sessionFactory.openSession();//创建会话对象
Session session2=sessionFactory.openSession();//创建会话对象
System.out.println(session1==session2);
/*
if(session!=null)
{
System.out.println("session创建成功");
}
else
{
System.out.println("创建失败");
}
*/
}
@Test
public void testGetCurrentSession()
{
Configuration config=new Configuration().configure();//获得配置对象
config.addClass(Students.class);
//获得服务注册对象。
ServiceRegistry serviceRegistry=new StandardServiceRegistryBuilder()
.applySettings(config.getProperties()).build(); //通过config.getProperties()读取配置文档。
//创建会话工厂对象
SessionFactory sessionFactory=config.buildSessionFactory(serviceRegistry);
Session session1=sessionFactory.getCurrentSession();//创建会话对象
Session session2=sessionFactory.getCurrentSession();//创建会话对象
System.out.println(session1==session2);
/*
if(session!=null)
{
System.out.println("session创建成功");
}
else
{
System.out.println("创建失败");
}
*/
} }
结果:false true
hbm配置文件常用设置
<hibernate-mapping>
schema="schemaName" //模式名
catelog="catelogName" //可以设置目录名称
default-cascade="cascade_style" //级联风格
default-access="filed|property|ClassName" //访问策略
default-lazy="true|false" //加载策略
package="packagename"
/>
<class
name="ClassName" //对象关系映射的类
table="tableName" //类所映射数据库中对应的表
batch-size="N" //抓取策略,每次抓取多少条数据
where="condition" //抓取数据的条件
entity-name="EntityName" //支持多个映射,同一个实体类可以映射成多个表
/>
<id //表示一个表的主键
name="propertyName" //主键要映射类的属性
type="typename" //数据类型
column="column_name" //映射到数据库中字段名
length="length" //指定长度
<generator class="generatorClass"/> //主键生成策略
</id>
主键生成策略
increment 自动递增
identity 由底层数据库生成
sequence 根据地底层数据库的序列生成标识符,要求底层数据库支持序列
hilo 分局high/low算法生成标识符
seqhilo 使用高/低算法的生成long,short或者int类型标识符
native 根据底层数据库对标识符生成标识符方式,自动选择identity,sequence或者hilo
uuid.hex 采用128为的uuid算法生成标识符
uuid.string uuid被编码成一个16字符长的字符串
assigned 适用自然主键,由java程序负责生成标识符
foreign 使用另外一个相关联对象的标识符。
08-session详解的更多相关文章
- PHP5 session 详解【经典】 -- 转帖
PHP5 session 详解[经典] http协议是WEB服务器与客户端(浏览器)相互通信的协议,它是一种无状态协议.所谓无状态,指的是不会维护http请求数据,http请求是独立的,非持久的.而越 ...
- 【Hibernate】Hibernate系列2之Session详解
Session详解 2.1.概述-一级缓存 2.2.操作session缓存方法 2.3.数据库隔离级别 2.4.持久化状态 2.5.状态转换 2.6.存储过程与触发器
- Cookie与Session详解
来源:<PHP核心技术与最佳实践> 列旭松 陈文 著 Cookie与Session详解读书笔记,从概念.操作.应用.注意事项以及区别等几方面详细阐述两者的基础知识,它们都是针对HTTP协议 ...
- orakill和ALTER SYSTEM KILL SESSION详解
--orakill和ALTER SYSTEM KILL SESSION详解[转]-----------------------------------------2013/11/05 一个用户进程偶尔 ...
- 巨人大哥谈Web应用中的Session(session详解)
巨人大哥谈Web应用中的Session(session详解) 虽然session机制在web应用程序中被采用已经很长时间了,但是仍然有很多人不清楚session机制的本质,以至不能正确的应用这一技术. ...
- 网络基础 http 会话(session)详解
http 会话(session)详解 by:授客 QQ:1033553122 会话(session)是一种持久网络协议,在用户(或用户代理)端和服务器端之间创建关联,从而起到交换数据包的作用机制 一. ...
- JavaWeb Session详解
代码地址如下:http://www.demodashi.com/demo/12756.html 记得把这几点描述好咯:代码实现过程 + 项目文件结构截图 + ## Session的由来 上一篇博文介绍 ...
- 引用 Session详解 作者:郎云鹏
本文转载自leeldy<Session详解 作者:郎云鹏> 引用 leeldy 的 Session详解 作者:郎云鹏 目录: 一.术语session 二.HTTP协议与状态保持 三.理 ...
- ASP.NET Session详解(转)
ASP.NET Session详解 本文章来自:http://blog.163.com/adam601@126/blog/static/22506317200932824210996/ 当用户在 We ...
- Session详解、ASP.NET核心知识(8)
介绍一下Session 1.作用 Cookie是存在客户端,Session是存在服务器端,目的是一样的:保存和当前客户端相关的数据(当前网站的任何一个页面都能取到Session). 在本篇博文的姊妹篇 ...
随机推荐
- matlab学习之求解函数的根和极小值
只是用来求解函数的部分一个根,具体算法没查询,只是调用自带的函数 代码如下 % 求函数零点和极小值 xiszero=func(0) x=-1:0.1:1.5; y=func(x); plot(x,y) ...
- Linux基础系列-系统密码破解
无引导介质(光盘.iso)救援模式下root密码破解 第一步: GRUB启动画面读秒时按上下方向键,进入GRUB界面 第二步: 使用上下光标键选择要修改的操作系统启动内核(默认选择的即可),按e键进行 ...
- [USACO 2018 Feb Gold] Tutorial
Link: USACO 2018 Feb Gold 传送门 A: $dp[i][j][k]$表示前$i$个中有$j$个0且末位为$k$的最优解 状态数$O(n^3)$ #include <bit ...
- poj 3225 Help with Intervals(线段树,区间更新)
Help with Intervals Time Limit: 6000MS Memory Limit: 131072K Total Submissions: 12474 Accepted: ...
- 【动态规划+高精度】mr360-定长不下降子序列
[题目大意] 韵哲君发现自己的面前有一行数字,当她正在琢磨应该干什么的时候,这时候,陈凡老师从天而降,走到了韵哲君的身边,低下头,对她耳语了几句,然后飘然而去. 陈凡老师说了什么呢,陈凡老师对韵哲君说 ...
- 【MySQL笔记】解除输入的安全模式,Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column To disable safe mode, toggle the option in Preferences -> SQL Queries and reconnect.
Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE tha ...
- Jenkins构建Maven多模块项目时,单独编译子模块,并且不触发构建其它模块
一.Jenkins构建Maven多模块项目时,单独编译子模块 配置: 1.Root POM指向父pom.xml 2.Goals and options指定构建模块的参数:mvn -pl jsoft-w ...
- [网络安全]NTP反射型DDOS攻击PPT
给运维小组培训的NTP反射型DDOS攻击的PPT
- <摘录>linux 默认的include
#include <linux/module.h> 中的module.h默认是在哪个目录下呢?我在/usr/include/linux下并没有找到这个文件. 另外想问一下,不同内核版本的l ...
- 定制你自己的CRF模型
如何训练自己的CRF模型 Ansj从5.0版本之后支持训练自己的crf模型,在此之前虽然已经很早支持了CRF模型,但是在用户的自训练上要求苛刻,而且本身实现的CRF从性能到准确率都不如CRF++或者第 ...