当前位置:网站首页>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{
关闭事务
}
边栏推荐
- QLabel 文字水平滚动显示
- OpenJudge NOI 2.1 15:Counterfeit Dollar
- What is Unified Extensible Firmware Interface (UEFI)?
- Day3 data types and operators summary and job
- character string
- [opencv450 samples] create image list yaml
- Typora writing DOS commands
- Actual combat: how to quickly change font color in typera (blog sharing - perfect) -2022.6.25 (solved)
- Oracle - data query
- 我的vscode
猜你喜欢

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

为什么OpenCV计算的帧率是错误的?

指针强化与提高

Paper notes: multi tag learning MSWl

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

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

How to add cartoon characters to the blog park?

ES6 - numerical extension and object extension

【opencv450-samples】inpaint 使用区域邻域恢复图像中的选定区域

1281_ FreeRTOS_ Implementation analysis of vtaskdelayuntil
随机推荐
【opencv450-samples】读取图像路径列表并保持比例显示
Transformers load pre training model
Xampp重启后,MySQL服务就启动不了。
UE4 学习记录二 给角色添加骨架,皮肤,及运动动画
OBS-Studio-27.2.4-Full-Installer-x64.exe 下载
Svn icon disappearing solution
golang Make a list of intervals with sequential numbers
cookie、session、token
After xampp restarts, the MySQL service cannot be started.
How to download the software package of CDH version
pdm导入vscode的实现方式
Recently prepared to translate foreign high-quality articles
Pycharm student's qualification expires, prompting no suitable licenses associated with account solution
String deformation (string case switching and realization)
指针强化与提高
元宇宙标准论坛成立
[modulebuilder] GP service realizes the intersection selection of two layers in SDE
Baidu: in 2022, the top ten hot spots will rise and the profession will be released. There is no suspense about the first place!
Live800 online customer service system: do business across time and space, starting from each interaction
电路模块分析练习6(开关)