当前位置:网站首页>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{
关闭事务
}
边栏推荐
- Pointer strengthening and improvement
- Common MySQL database functions and queries
- Set up your own website (15)
- 【AXI】解读AXI协议乱序机制
- RepOptimizer: 其实是RepVGG2
- How to add cartoon characters to the blog park?
- Comp2913 database
- ES6 -- formal parameter setting initial value, extension operator, iterator, and generating function
- Circuit module analysis exercise 5 (power supply)
- Technology blog site collection
猜你喜欢

RepOptimizer: 其实是RepVGG2

qtcreator 格式化代码

Xinchida nd04 nd04c nrf52832 (52810) ble module (low power Bluetooth communication module) at command test

Pit resolution encountered using East OCR (compile LAMS)

How to add cartoon characters to the blog park?

leetcode_ 136_ A number that appears only once

UE4\UE5 蓝图节点Delay与Retriggerable Delay的使用与区别

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

电路模块分析练习6(开关)

元宇宙标准论坛成立
随机推荐
UE4 学习记录二 给角色添加骨架,皮肤,及运动动画
Go语言逃逸分析全纪录
pdm导入vscode的实现方式
【无标题】打开一个项目连接,无法正常显示时,ping一下ip
判断预约时间是否已经过期
Transformers load pre training model
电路模块分析练习6(开关)
Oracle -- table operation
How to solve the problem of SQL?
As a programmer, how can we learn, grow and progress happily? (personal perception has nothing to do with technology)
记录一下Qt将少量图片输出为MP4的思路及注意事项
Idea auto generator generates constructor get/set methods, etc
小程序-视图与逻辑
STM32 development board + smart cloud aiot+ home monitoring and control system
树状类查询组件
The sum of logarithms in group 52--e of Niuke Xiaobai monthly race (two points)
Flex & Bison Start
cookie、session、token
统计字符串中不同回文子序列的个数
MySQL queries data by day, week, month, quarter and year