当前位置:网站首页>hiberate核心API/配置文件/一级缓存详解
hiberate核心API/配置文件/一级缓存详解
2022-06-25 22:01:00 【_七里香】
挺长,也详细。
如有帮助,欢迎留下足迹哦!
目录
核心API
Configuration
加载配置文件
SessionFactory
使用Configuration对象可以创建session工厂对象;根据核心配置文件中数据库的配置、映射文件部分来创建表;创建SessionFactory过程很消耗资源,建议一个项目只创建一个SessionFactory对象,即一个工具类即可。
具体实现:静态代码块,类加载时执行且只执行一次;

Session
类似于JDBC中的Connection。session是单线程对象,即只能自己用,不能共用,每次只能做一次。
Transaction
两个操作,一个commit(),一个rollback()
事务的四个特性:
原子性:事务要么都成功,要么都失败。
一致性:操作前后总量不变,如转账操作一个人多钱一个人少钱。
隔离性:多个事务同时操作同一个记录,各不影响。
持久性:最终事务要提交,commit到数据库。
配置文件详解
核心配置文件
1、数据库的信息配置是必须的,配置hibernate信息可有可无,注册映射文件必须的。
2、<hibernate-configuration>
<session-factory>注意位置。
3、必须在src下,且名称固定
映射配置文件
1、位置不固定,名称不固定,但不能是中文名
2、<properties name=”” column=””>,name中写的都必须与实体类的属性对应一致,column中写的都是数据库表中的列。class标签的name属性写的是全限定名(由类名右键复制可得到),table属性是将会生成的表名。
3、<properties name=”” column=””>标签中如果不写column属性,那么生成的字段名将会和name属性一致。
4、<properties name=”” column=”” type=””>标签的type属性一般不写,type属性用于设置生成表的字段的类型,自动根据实体类属性的类型转换为数据库所对应类型,不需要手动写。
Hibernate的一级缓存
解释
数据存到数据库中,数据库本身是文件系统。用流的方式操作文件效率不是很高
因此:
将数据存到内存中,此时不使用流的方式,可以直接读取内存中的内容。
把数据放到内存中,提高读取效率。
Hibernate框架中提供了很多优化方式:Hibernate一级缓存就是一个优化机制。
Hibernate缓存特点
一级缓存:默认就是打开的。使用范围是session的范围(session的创建-关闭之间),即session之外一级缓存是不存在的。
存储的数据必须是一种状态的数据--持久态的数据。
二级缓存:已经不使用了。有一种替代技术,被redis替代。默认不是打开的。使用范围是整个项目的范围(SessionFactory)。
验证一级缓存操作
<1>根据userid=1查询,返回对象
<2>再根据userid=1查询,返回对象,此时不会产生sql语句,可以验证一级缓存的存在。


一级缓存的执行过程
User u1 = session.get(User.class, 14);
执行时,先查询缓存中是否有数据,如果有则返回,如果没有就去查询数据库返回User对象(持久态的对象);同时将该持久态的对象放到一级缓存中(同时将一样的对象放到快照区),此时一级缓存中已有数据。
当第二次查询该目标时,仍然先去查询缓存看是否有数据(根据id值),如果有直接从一级缓存中获取数据;第二次查询时查到的虽然是同样的值但是会组成不同的对象。
一级缓存的存储结构
不是存储整个对象的形式,而是只存储对象的值,如userid=14,username=多少等;
一级缓存的特性及原理
修改操作:验证一级缓存的特性:持久态会自动更新数据库,不需要调用update()方法

原理:
创建session时,会创建一块一级缓存区,同时创建一块一样的区域--快照区(副本);当执行u1.setPassword("mimmmm");时,会将一级缓存中的user值进行修改,快照区里的user值不变,数据库中也不变;要修改的值没有变化时对数据库不做更改,性能很高。
即,一级缓存在事务提交之前代表用户的操作,快照区中的值代表的是数据库中现在的值,在提交事务时会比较一级缓存中的内容与快照区中的值是否一致,如果一致则不做修改,如果不一致则按一级缓存中的内容来更新数据库,此时快照区的值同时更新。

Hibernate的事务操作
事务代码的规则写法
try{
开启事务
提交事务
}catch(){
回滚事务
}finally{
关闭事务
}
边栏推荐
- [opencv450 samples] read the image path list and maintain the proportional display
- NLP pre training model-2018:bert dictionary
- 软件测试面试一直挂,面试官总是说逻辑思维混乱,怎么办?
- Oracle -- table operation
- ACM. HJ16 购物单 ●●
- UE4_UE5結合offline voice recognition插件做語音識別功能
- RK3568+鸿蒙工控板工业网关视频网关解决方案
- The sum of logarithms in group 52--e of Niuke Xiaobai monthly race (two points)
- pdm的皮毛
- Beacon realizes asset management and indoor positioning based on 5.2 ultra-low power Bluetooth module efr32 (bg22ax)
猜你喜欢

Paper notes: multi tag learning MSWl
![[opencv450 samples] inpaint restores the selected region in the image using the region neighborhood](/img/36/8ad6034473382f66f315eb70440711.png)
[opencv450 samples] inpaint restores the selected region in the image using the region neighborhood

How to solve the problem of SQL?

CTS RTS RX TX in serial port flow control UART (direct communication between serial port module and MCU)

软件测试面试一直挂,面试官总是说逻辑思维混乱,怎么办?

UE4_UE5结合offline voice recognition插件做语音识别功能

konva系列教程2:绘制图形
2. What is the geometric meaning of a vector multiplying its transpose?

UE4_UE5結合offline voice recognition插件做語音識別功能

23class introduction
随机推荐
Ue4 Ue5 combine le plug - in de reconnaissance vocale de bureau pour la reconnaissance vocale
电路模块分析练习6(开关)
Pit resolution encountered using East OCR (compile LAMS)
Ble Low Power Bluetooth networking process and Bluetooth role introduction
Problem recording and thinking
Multithreaded learning 1
24class static member
字符串
OpenJudge NOI 2.1 15:Counterfeit Dollar
23class introduction
leetcode_ 136_ A number that appears only once
Oracle - data query
To solve the incompatibility between VM and device/credential guard, an effective solution for the whole network
Core points of assembly language
Transformers load pre training model
Meta universe standard forum established
ES6 learning -- let
记一次beego通过go get命令后找不到bee.exe的坑
Live800 online customer service system: do business across time and space, starting from each interaction
2. What is the geometric meaning of a vector multiplying its transpose?