hibernate中3个重要的类 Configuration SessionFactory Session
配置类Configuration
主要负责管理hibernate的配置信息以及启动hibernate,在hibernate运行时,配置文件取读底层的配置信息,基本包括数据库驱动,url、username、password、dialect、show_sql、format_sql、mapping映射文件等等
配置文件一般放在src目录下
demo:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration 3.0.dtd"> <hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.username">root</property> <property name="connection.password">root</property> <property name="connection.url">jdbc:mysql://localhost:3306/helloworld</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="show_sql">true</property> <property name="hibernate.format_sql">true</property> <mapping resource="com/hibernate/bean/Student.hbm.xml"></mapping> </session-factory>
</hibernate-configuration>
会话工厂类SessionFactory
会话工厂是生成Session的工厂,保存了当前数据库中所有的映射关系,可能只有一个可选的二级缓存,线程安全。它是一个重量级对象,消耗大量系统资源
生成SessionFactory
Configuration cfg=new Configuration().configure();
SessionFactory sessionFactory=cfg.bulidSessionFactory();
可以将生成的SessionFactory对象封装起来,后面使用的时候直接使用getter方法调用,减少系统资源的损耗。
Session会话类
这个Session不是JSP中的Session内置对象了,它是会话类,由SessionFactory创建。是数据库持久化操作的核心,负责hibernate的所有持久化操作,通过它执行数据库增删改查等操作。会话类不是线程安全,不要多会话共享一个Session。
创建session
Session session=sessionFactory.openSession();
获取会话中的Session
private final ThreadLocal<Session> threadLocal=new ThreadLocal<Session>();
Session session=(Session) threadLocal.get();
所以获取Session的方法
public Session getSession() throws HibernateException{
Session session=(Session)threadLocal.get();
if(session==null||!session.isOpen()){
session=(sessionFactory!=null)?sessionFactory.openSession():null;
threadLocal.set(session);
}
return session;
}
hibernate中3个重要的类 Configuration SessionFactory Session的更多相关文章
- Hibernate中的条件查询完成类
Hibernate中的条件查询有以下三个类完成: 1.Criteria:代表一次查询 2.Criterion:代表一个查询条件 3.Restrictions:产生查询条件的工具类
- Hibernate中的条件查询完毕类
Hibernate中的条件查询有下面三个类完毕: 1.Criteria:代表一次查询 2.Criterion:代表一个查询条件 3.Restrictions:产生查询条件的工具类
- Hibernate学习---Configuration,Session,SessionFactory
上一节我们讲到了Hibernate的测试,并且给出了测试代码,刚开始看见这个测试代码的同学估计是一头雾水把,所以这一节我们来讲一下测试代码. 本节主要内容: Configuration Session ...
- hibernate 中文文档
转载:http://blog.csdn.net/kevon_sun/article/details/42850387 Hibernate Annotations 参考文档 3.2.0 CR1 目录 前 ...
- JavaWeb_(Hibernate框架)Hibernate中重要的api
Hibernate中重要的api Configuration SessionFactory Session(重点) Transaction 在Dao层中UserDao.java使用Hibernate向 ...
- 在Hibernate中分别使用JDBC和JTA事务的方法
在Hibernate中使用JDBC事务 Hibernate对JDBC进行了轻量级的封装,它本身在设计时并不具备事务处理功能.Hibernate将底层的JDBCTransaction或JTATransa ...
- Java Hibernate中的悲观锁和乐观锁的实现
锁(locking) 业务逻辑的实现过程中,往往需要保证数据访问的排他性.如在金融系统的日终结算 处理中,我们希望针对某个cut-off时间点的数据进行处理,而不希望在结算进行过程中 (可能是几秒种, ...
- Hibernate 中的锁( locking )
业务逻辑的实现过程中,往往需要保证数据访问的排他性.如在金融系统的日终结算处理中,我们希望针对某个 cut-off 时间点的数据进行处理,而不希望在结算进行过程中(可能是几秒种,也可能是几个小时),数 ...
- (七)Hibernate中使用JDBC
在hibernate中获取connection数据库连接有两种方法:(操作数据库常用这种方法) 1. session.doReturningWork 返回一个对象,适用于查询方法 2. sessi ...
随机推荐
- The DELETE statement conflicted with the REFERENCE constraint
Page是主表,主键是pageid:UserGroupPage表中的PageID字段是Page表里的数据. https://www.codeproject.com/Questions/677277/I ...
- 论文笔记——Deep Model Compression Distilling Knowledge from Noisy Teachers
论文地址:https://arxiv.org/abs/1610.09650 主要思想 这篇文章就是用teacher-student模型,用一个teacher模型来训练一个student模型,同时对te ...
- .Net Core EF Core之Sqlite使用及部署
1.添加引用Nuget包 Microsoft.EntityFrameworkCore.Sqlite Microsoft.EntityFrameworkCore.Design Microsoft.Ent ...
- caffe深度学习网络(.prototxt)在线可视化工具:Netscope Editor
http://ethereon.github.io/netscope/#/editor 网址:http://ethereon.github.io/netscope/#/editor 将.prototx ...
- 解决复制到keil编辑器中汉字出现乱码情况
https://blog.csdn.net/dxuehui/article/details/51123372 1.在菜单栏中选择'Edit'选项. 2.'Edit'选项中选择'Configuratio ...
- MongoDB(课时26 聚合(取的集合个数))
3.7 聚合(重点) 信息的统计操作就是聚合(直白:分组统计就是一种聚合操作). 3.7.1 取的集合的数据量 对于集合的数据量而言,在MongoDB里面直接使用count()函数就可以完成. 范例: ...
- [ios][switf]页面跳转
参考:http://bbs.csdn.net/topics/390899712 注意用push会崩溃 用其他的正常 1.storyboard直接拖拉,使用不同种类的segue均可2.直接写代码: // ...
- Spark与Flink大数据处理引擎对比分析!
大数据技术正飞速地发展着,催生出一代又一代快速便捷的大数据处理引擎,无论是Hadoop.Storm,还是后来的Spark.Flink.然而,毕竟没有哪一个框架可以完全支持所有的应用场景,也就说明不可能 ...
- 30分钟带你了解Springboot与Mybatis整合最佳实践
前言:Springboot怎么使用想必也无需我多言,Mybitas作为实用性极强的ORM框架也深受广大开发人员喜爱,有关如何整合它们的文章在网络上随处可见.但是今天我会从实战的角度出发,谈谈我对二者结 ...
- idea运行main方法报错,提示Shorten command line for xxx
在Intell IDEA运行main函数的时候遇到了如下错误: Error running' xxxxxx': Command line is too long. Shorten command li ...