当前位置:网站首页>Addition and modification of JPA
Addition and modification of JPA
2022-06-13 00:56:00 【-LM-】
Problems encountered
Usually , When we add and modify, we are based on whether the value passed in to the background has id Value to judge . But now use JPA You will find that even when we make changes, it is transferred to the background id value , A new record will also be added to the database , Instead of modifying . This is mainly because JPA The bottom layer judges the addition and modification based on version Field to judge , But we don't have any in the incoming data version Field , Therefore, the new operation is always performed .
Source code analysis
JPA Whether you are executing an add method or a modify method , All the calls are save This method , So this requires save Method body to determine when to add , When to modify . Let's take a look through the debugging program JPA At the bottom save How to achieve .
1、 Get into SimpleJpaRepository Methods save Method .

2、save Method source code
By looking at save Method source code can be seen , It judges in the first sentence entity Is it a new test question , If it is a new one, it should be stored persistently , If not, merge the original data . Let's mainly look at isNew Method implementation .
@Transactional
public <S extends T> S save(S entity) {
if (entityInformation.isNew(entity)) {
em.persist(entity);
return entity;
} else {
return em.merge(entity);
}
}
3、 find isNew Method
We enter JpaMetamodelEntityInformation Of isNew In the method .
Class inheritance structure diagram 
4、isNew Method source code
You can find out by looking at the source code , This method is mainly based on version To determine whether to update the data , The main problem is here , If we have to update the data in the entity class version Field , At the same time, when the front end transmits data to the background, you need to bring version Field , If not, it is a new operation . The concrete realization is , This method first determines whether there is version Attributes exist , After that, get the wrapper of the instance bean, By packing bean To determine the version Whether the attribute value is NULL.
@Override
public boolean isNew(T entity) {
// To determine if there is version Attributes exist
if (!versionAttribute.isPresent()
|| versionAttribute.map(Attribute::getJavaType).map(Class::isPrimitive).orElse(false)) {
return super.isNew(entity);
}
// Get the wrapper of the instance bean
BeanWrapper wrapper = new DirectFieldAccessFallbackBeanWrapper(entity);
// By packing bean To determine the version Whether the attribute value is NULL
return versionAttribute.map(it -> wrapper.getPropertyValue(it.getName()) == null).orElse(true);
}
边栏推荐
- People and gods are angry. Details of Tangshan "mass beating of women incident"
- Paper reading and sharing
- 单片机串口中断以及消息收发处理——对接受信息进行判断实现控制
- Development notes of Mongoose
- 408 true question - division sequence
- Androi weather
- 什么是 Meebits?一个简短的解释
- How many steps are appropriate for each cycle of deep learning?
- [JS component] customize the right-click menu
- Dynamic planning - good article link
猜你喜欢
![[JS component] simulation framework](/img/f2/8d5bb7e0db55a87ce76c09fae03694.jpg)
[JS component] simulation framework

阿姨学代码续集:能力吊打大批程序员

How many steps are appropriate for each cycle of deep learning?

Kotlin coroutine suspend function suspend keyword

通过抓包下载钉钉直播回放

Druid reports an error connection holder is null
![[JS component] dazzle radio box and multi box](/img/2a/00620bee312972db93e1db4313385f.jpg)
[JS component] dazzle radio box and multi box

Breadth first search for node editor runtime traversal

Deep learning model pruning

(01).NET MAUI实战 建项目
随机推荐
今日在家休息
[virtual machine] notes on virtual machine environment problems
今日睡眠质量记录74分
Four startup modes of kotlin collaboration
Canvas random bubbling background
People and gods are angry. Details of Tangshan "mass beating of women incident"
Quantitative investment traditional index investment decision vs Monte Carlo simulation method
[JS component] previous queue prompt
Set sail
Aunt learning code sequel: ability to sling a large number of programmers
[Latex] 插入图片
Get preview of precast body
[buglist] serial port programming does not read data
kotlin 协程withContext切换线程
Biological unlocking - Fingerprint entry process
What is dummy change?
[server data recovery] successful cases of data loss recovery during data migration between storage servers
Three column simple Typecho theme lanstar/ Blue Star Typecho theme
pytorch是什么?解释pytorch的基本概念
Physical orbit simulation