如何获取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详解的更多相关文章

  1. PHP5 session 详解【经典】 -- 转帖

    PHP5 session 详解[经典] http协议是WEB服务器与客户端(浏览器)相互通信的协议,它是一种无状态协议.所谓无状态,指的是不会维护http请求数据,http请求是独立的,非持久的.而越 ...

  2. 【Hibernate】Hibernate系列2之Session详解

    Session详解 2.1.概述-一级缓存 2.2.操作session缓存方法 2.3.数据库隔离级别 2.4.持久化状态 2.5.状态转换 2.6.存储过程与触发器

  3. Cookie与Session详解

    来源:<PHP核心技术与最佳实践> 列旭松 陈文 著 Cookie与Session详解读书笔记,从概念.操作.应用.注意事项以及区别等几方面详细阐述两者的基础知识,它们都是针对HTTP协议 ...

  4. orakill和ALTER SYSTEM KILL SESSION详解

    --orakill和ALTER SYSTEM KILL SESSION详解[转]-----------------------------------------2013/11/05 一个用户进程偶尔 ...

  5. 巨人大哥谈Web应用中的Session(session详解)

    巨人大哥谈Web应用中的Session(session详解) 虽然session机制在web应用程序中被采用已经很长时间了,但是仍然有很多人不清楚session机制的本质,以至不能正确的应用这一技术. ...

  6. 网络基础 http 会话(session)详解

    http 会话(session)详解 by:授客 QQ:1033553122 会话(session)是一种持久网络协议,在用户(或用户代理)端和服务器端之间创建关联,从而起到交换数据包的作用机制 一. ...

  7. JavaWeb Session详解

    代码地址如下:http://www.demodashi.com/demo/12756.html 记得把这几点描述好咯:代码实现过程 + 项目文件结构截图 + ## Session的由来 上一篇博文介绍 ...

  8. 引用 Session详解 作者:郎云鹏

    本文转载自leeldy<Session详解 作者:郎云鹏>   引用 leeldy 的 Session详解 作者:郎云鹏 目录: 一.术语session 二.HTTP协议与状态保持 三.理 ...

  9. ASP.NET Session详解(转)

    ASP.NET Session详解 本文章来自:http://blog.163.com/adam601@126/blog/static/22506317200932824210996/ 当用户在 We ...

  10. Session详解、ASP.NET核心知识(8)

    介绍一下Session 1.作用 Cookie是存在客户端,Session是存在服务器端,目的是一样的:保存和当前客户端相关的数据(当前网站的任何一个页面都能取到Session). 在本篇博文的姊妹篇 ...

随机推荐

  1. 关于phonegap的白名单机制

    今天在项目中发现了一个问题,使用phonegap开发的APP默认情况下可以将外部网页加载进入手机APP当中,这是相当危险的,同时也会给人一种APP非native的感觉. 可能遇见的一种情况是有些WiF ...

  2. React 中组件的生命周期

    先上代码, react 用的是 15.0.1 <!DOCTYPE html> <html> <head> <script src="./build/ ...

  3. 【BZOJ 1084】 1084: [SCOI2005]最大子矩阵 (DP)

    1084: [SCOI2005]最大子矩阵 Description 这里有一个n*m的矩阵,请你选出其中k个子矩阵,使得这个k个子矩阵分值之和最大.注意:选出的k个子矩阵不能相互重叠. Input 第 ...

  4. 【数形结合】Erratic Expansion

    [UVa12627]Erratic Expansion 算法入门经典第8章8-12(P245) 题目大意:起初有一个红球,每一次红球会分成三红一蓝,蓝球会分成四蓝(如图顺序),问K时的时候A~B行中有 ...

  5. 【模拟+递归+位运算】POJ1753-Flip Game

    由于数据规模不大,利用爆搜即可.第一次用位运算写的,但是转念一想应该用递归更加快,因为位运算没有剪枝啊(qДq ) [思路] 位运算:时间效率较低(172MS),有些辜负了位运算的初衷.首先将二维数组 ...

  6. bzoj 1783: [Usaco2010 Jan]Taking Turns

    1783: [Usaco2010 Jan]Taking Turns Description Farmer John has invented a new way of feeding his cows ...

  7. Java字节码文件结构---概述

    一.Class文件的结构概述: 是一连串的字节流(以自节为基本单位划分),里面包含的数据项按照固定的次序依次排列组成Class文件,文件内部不含分割符 当数据项的长度大于1B时候,按照高位在前的方式存 ...

  8. Educational Codeforces Round 9 C. The Smallest String Concatenation 排序

    C. The Smallest String Concatenation 题目连接: http://www.codeforces.com/contest/632/problem/C Descripti ...

  9. Manthan, Codefest 16 E. Startup Funding ST表 二分 数学

    E. Startup Funding 题目连接: http://codeforces.com/contest/633/problem/E Description An e-commerce start ...

  10. SqlServer查看对象(表、存储过程、函数)在哪些地方被引用或引用哪些地方

    对象(如表)-->右键-->查看依赖关系 依赖于[]的对象:列出哪些存储过程.函数引用了该表 []依赖的对象:列出该存储过程.函数依赖了哪些表.函数.存储过程