EntityManager的Clear方法的使用
在日常开发中,如果使用hibernate的话,常常会被hibernate的事务搞得焦头烂额。今天解决了之前项目中一直存在的问题,记录一下。
问题描述
有一张表TemplateCopy,如下
public class TemplateCopy {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
private String description;
@OneToMany(mappedBy = "template")
private Set<SubDomainWeightsCopy> subDomainWeights;
@OneToMany(mappedBy = "template")
private Set<QuestionWeightsCopy> questionWeights;
}
关联了两张表:
public class SubDomainWeightsCopy {
@JsonIgnore
@Id
@ManyToOne
@JoinColumn(name = "template_id")
private TemplateCopy template;
@Id
@ManyToOne
@JoinColumn(name = "sub_domain_id")
private SubDomainCopy subDomain;
private BigDecimal weights; //权重
private BigDecimal score;
@Data
public static class RelationId implements Serializable {
private Integer template;
private Integer subDomain;
}
}
public class QuestionWeightsCopy implements IWeightsValue {
@JsonIgnore
@Id
@ManyToOne
@JoinColumn(name = "template_id")
private TemplateCopy template;
@Id
@ManyToOne
@JoinColumn(name = "question_id")
private QuestionCopy question;
private BigDecimal weights;
private BigDecimal score;
@Data
public static class RelationId implements Serializable {
private Integer template;
private Integer question;
}
}
简单的看一下,TemplateCopy中有一堆SubDomainWeightsCopy,和一堆QuestionWeightsCopy,我们在保存TemplateCopy的时候,通常按照如下来保存
1. templateCopy = save(TemplateCopy)
2. QuestionWeightsCopy.setTemplateCopy(templateCopy)
3. save(QuestionWeightsCopy)
4. SubDomainWeightsCopy.setTemplateCopy(templateCopy)
5. save(SubDomainWeightsCopy)
到这就好了,数据库已经保存了关联关系。但是,这时候如果返回save好的templateCopy,subDomainWeights和questionWeights将会是null。
问题解决
使用EntityManager的clear方法
- 保存完毕后,执行entityManager.clear();
- 然后再次查询该对象,即可完整返回该对象。
EntityManager clear的作用?
EntityManager clear方法会清空其关联的缓存,从而强制在事务中稍后执行新的数据库查询。
什么时候使用EntityManager clear
- 在进行批处理时,为了避免巨大的缓存占用内存并因长时间的脏检查而增加刷新的时间
- 在进行DML或SQL查询时,它将完全绕过实体管理器缓存。在这种情况下,由于缓存,将不会实际去数据库查,会直接将缓存返回。所以造成了数据库已经保存了,但是查出来还是未保存的状态。这时候需要清除缓存以避免这种不一致。(本案例就是这种情况的实际例子)
参考
EntityManager的Clear方法的使用的更多相关文章
- List集合的removeAll(Collection<E> col) 和clear方法的区别
//removeAll()方法private static void testList(){ List<String> list = new ArrayList<String> ...
- java.nio.ByteBuffer中flip,rewind,clear方法的区别
对缓冲区的读写操作首先要知道缓冲区的下限.上限和当前位置.下面这些变量的值对Buffer类中的某些操作有着至关重要的作用: limit:所有对Buffer读写操作都会以limit变量的值作为上限. p ...
- Hibernate中evict方法和clear方法说明
Hibernate中evict方法和clear方法说明 先创建一个对象,然后调用session.save方法,然后调用evict方法把该对象清除出缓存,最后提交事务.结果报错: Exception i ...
- delphi 中TStringList Clear 方法的时候该对象有没有被释放
delphi 中TStringList 通过function AddObject(const S: string; AObject: TObject): Integer; 方法添加了一个对象,请问我在 ...
- List集合的clear方法
一 . list.clear()底层源码实现 在使用list 结合的时候习惯了 list=null :在创建这样的方式,但是发现使用list的clear 方法很不错,尤其是有大量循环的时候 1.lis ...
- Selenium clear()方法无法清掉数据
问题描述 clear()方法执行过后, 数据还是在. 根本原因 存在镜像节点. 操作clear()清掉数据后, 镜像节点的数据还在, 就会再补充回去. 解决办法 添加下面代码就可以连同镜像的数据一起去 ...
- Python 字典 clear()方法
描述 Python 字典 clear() 方法用于删除字典内所有元素. 语法 clear() 方法语法: D.clear() 参数 无. 返回值 该方法没有任何返回值. 实例 以下实例展示了 clea ...
- Python3 列表 clear() 方法
描述 Python3 列表 clear() 方法用于清空列表,类似于 del a[:]. 语法 clear() 方法语法: L.clear() 参数 无. 返回值 该方法没有返回值. 实例 以下实例展 ...
- Python3 字典 clear()方法
Python3 字典 描述 Python 字典 clear() 函数用于删除字典内所有元素. 语法 clear()方法语法: dict.clear() 参数 NA. 返回值 该函数没有任何返回值. ...
随机推荐
- CCF_201312-1_出现次数最多的数
水. #include<stdio.h> int main() { ,a[]={},num[]={}; scanf("%d",&T); ;i < T;i+ ...
- Codeforces 922 C - Robot Vacuum Cleaner (贪心、数据结构、sort中的cmp)
题目链接:点击打开链接 Pushok the dog has been chasing Imp for a few hours already. Fortunately, Imp knows that ...
- why NW NMM backup sqlserver failed and how to solve it
A NW NMM backup sqlserver failed. wow , I realze that maybe I put too many database backup together ...
- 使用newtonsoft完美序列化WebApi返回的ValueTuple
由于开发功能的需要,又懒得新建太多的class,所以ValueTuple是个比较好的偷懒方法,但是,由于WebApi需要返回序列化后的json,默认的序列化只能将ValueTuple定义的各个属性序列 ...
- Python趣味入门02: 妥妥地安装配置Python(Windows版)
< 上一篇:Python趣味入门01:你真的了解Python么? 本篇内容手把手教您如何去网上下载安装Python的运行环境,本文写于2020年Python稳定的版本是3.8,Windows流行 ...
- c++头文件包含 #ifndef ##pragma once
2013-04-14 17:03 (分类:计算机程序) 烦死了,这种垃圾小问题很多,你又必须要知道.......在编写c++程序时,会编写多个类或者多个cpp文件,免不了要多次使用include包含头 ...
- 1215 - Finding LCM
1215 - Finding LCM LCM is an abbreviation used for Least Common Multiple in Mathematics. We say LC ...
- 浅谈 k8s ingress controller 选型
大家好,先简单自我介绍下,我叫厉辉,来自腾讯云.业余时间比较喜欢开源,现在是Apache APISIX PPMC.今天我来简单给大家介绍下 K8S Ingress 控制器的选型经验,今天我讲的这些内容 ...
- Docker深入浅出系列 | 容器数据持久化
Docker深入浅出系列 | 容器数据持久化 Docker已经上市很多年,不是什么新鲜事物了,很多企业或者开发同学以前也不多不少有所接触,但是有实操经验的人不多,本系列教程主要偏重实战,尽量讲干货,会 ...
- Linux运维---02.制作trove-redis镜像
redis-3.2 镜像制作及验证 镜像制作 1.安装redis yum install redis yum install epl-release yum install python-pip gi ...