当前位置:网站首页>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{
关闭事务
}
边栏推荐
- Baidu: in 2022, the top ten hot spots will rise and the profession will be released. There is no suspense about the first place!
- Leaky API interface practical development series (13): gooseneck cloud service php-api two-dimensional array parameter transfer solution
- Go语言逃逸分析全纪录
- #23class介绍
- Flex & Bison 開始
- 为什么OpenCV计算的帧率是错误的?
- 判断预约时间是否已经过期
- 【opencv450-samples】读取图像路径列表并保持比例显示
- Qt 中文和英文分别使用不同的字体
- 2. What is the geometric meaning of a vector multiplying its transpose?
猜你喜欢

What aspects should we start with in the feasibility analysis of dry goods?
![[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

Kubernetes cluster construction of multiple ECS

元宇宙标准论坛成立

How to solve the problem of SQL?

STM32 development board + smart cloud aiot+ home monitoring and control system

LM small programmable controller software (based on CoDeSys) note XVII: PTO pulse function block

Oracle - getting started

问题记录与思考

Idea shortcut
随机推荐
漏刻有时API接口实战开发系列(13):小鹅通云服务PHP-API二维数组传参解决方案
Flex & Bison Start
Windows redis installation and simple use
pdm导入vscode的实现方式
The sum of logarithms in group 52--e of Niuke Xiaobai monthly race (two points)
UE4 学习记录一 创建角色,并控制其移动
Use and difference between ue4\ue5 blueprint node delay and retroggable delay
Idea auto generator generates constructor get/set methods, etc
电路模块分析练习6(开关)
Live800 online customer service system: do business across time and space, starting from each interaction
Windows安装Redis及简单使用
To solve the incompatibility between VM and device/credential guard, an effective solution for the whole network
STM32开发板+机智云AIoT+家庭监测控制系统
After xampp restarts, the MySQL service cannot be started.
[opencv450 samples] create image list yaml
最近准备翻译外国优质文章
STM32 development board + smart cloud aiot+ home monitoring and control system
Actual combat: how to quickly change font color in typera (blog sharing - perfect) -2022.6.25 (solved)
Idea shortcut
go中全类型等值判断解析&综合汇总