当前位置:网站首页>hiberate实体类CURD、事务操作汇总
hiberate实体类CURD、事务操作汇总
2022-06-25 22:01:00 【_七里香】
满满的干货哦!
目录
添加操作
save()方法


saveOrUpdate()方法

查询

结果:

修改

使用save()方法与此效果完全一致:

不建议直接new对象再调update()方法,这样会产生空值。
删除操作
第一种:根据ID查询,返回对象,调用delete()方法删除该对象即可

第二种:new对象,调用delete()方法删除该对象即可
保存或更新
saveOrUpdate()
瞬时态时:该方法做添加操作

持久态时:修改操作

托管态时:修改操作

工具类封装
package com.wl.utils;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/*
* 抽取工具类
*/
public class HibernateUtils {
private static final Configuration cfg ;
private static final SessionFactory sessionFactory ;
static{
cfg = new Configuration();
cfg.configure();
sessionFactory = cfg.buildSessionFactory();
}
//该方法返回这个sessionFactory对象
public static SessionFactory getSessionFactory(){
return sessionFactory;
}
}
实体类的对象状态
瞬时态
对象中没有id值,对象与session没有关联。
持久态
对象中有id值,对象与session关联
托管态
对象中有id值,对象与session没有关联
Hibernate的事务操作
事务代码的规则写法
try{
开启事务
提交事务
}catch(){
回滚事务
}finally{
关闭事务
}
边栏推荐
- 【无标题】打开一个项目连接,无法正常显示时,ping一下ip
- 记录一下Qt将少量图片输出为MP4的思路及注意事项
- STM32 development board + smart cloud aiot+ home monitoring and control system
- Pointer strengthening and improvement
- Oracle -- table operation
- CTS RTS RX TX in serial port flow control UART (direct communication between serial port module and MCU)
- As a programmer, how can we learn, grow and progress happily? (personal perception has nothing to do with technology)
- 论文笔记: 多标签学习 MSWL
- 判断预约时间是否已经过期
- 多台云服务器的 Kubernetes 集群搭建
猜你喜欢

Baidu: in 2022, the top ten hot spots will rise and the profession will be released. There is no suspense about the first place!

【无标题】打开一个项目连接,无法正常显示时,ping一下ip

Repoptimizer: it's actually repvgg2

Xampp重启后,MySQL服务就启动不了。

记一次beego通过go get命令后找不到bee.exe的坑

Fegin client entry test

漏刻有时API接口实战开发系列(13):小鹅通云服务PHP-API二维数组传参解决方案

Leaky API interface practical development series (13): gooseneck cloud service php-api two-dimensional array parameter transfer solution

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

干货丨产品的可行性分析要从哪几个方面入手?
随机推荐
CTS RTS RX TX in serial port flow control UART (direct communication between serial port module and MCU)
小程序-视图与逻辑
Count the number of different palindrome subsequences in the string
No absurd tea applet - rule change
How to download the software package of CDH version
统计字符串中不同回文子序列的个数
My C language learning process
【opencv450-samples】inpaint 使用区域邻域恢复图像中的选定区域
String对象(常量)池
Typora writing DOS commands
golang Make a list of intervals with sequential numbers
Core points of assembly language
The sum of logarithms in group 52--e of Niuke Xiaobai monthly race (two points)
CAD中图纸比较功能怎么用
Jupiter notebook common shortcut keys
元宇宙标准论坛成立
STM32开发板+机智云AIoT+家庭监测控制系统
Why is BeanUtils not recommended?
. SQL database import error: / *! 40101 SET @OLD_ COLLATION_ [email protected]@COLLATION_ CONNECTION */
ES6 -- formal parameter setting initial value, extension operator, iterator, and generating function