当前位置:网站首页>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);
}
边栏推荐
- kotlin 协程withContext切换线程
- Hard (magnetic) disk (I)
- Jenkins持续集成操作
- How to determine whether T is a value type in a generic type or a reference class- How to determine whether T is a value type or reference class in generic?
- Common skills of quantitative investment - index part 2: detailed explanation of BOL (Bollinger line) index, its code implementation and drawing
- Druid reports an error connection holder is null
- Introduction to ROS from introduction to mastery (zero) tutorial
- 今日睡眠质量记录74分
- [imx6ull] video monitoring project (USB camera +ffmepeg)
- Sequence table - find main element
猜你喜欢

Penetration test summary

Unity calls alertdialog

Undirected graph -- computing the degree of a node in compressed storage

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

Arduino control soil moisture sensor

Common skills of quantitative investment - index part 2: detailed explanation of BOL (Bollinger line) index, its code implementation and drawing

Win10 home vs pro vs enterprise vs enterprise LTSC

Influence of higher order poles on waveform

市值破万亿,连续三个月销量破10万,比亚迪会成为最强国产品牌?

什么是 dummy change?
随机推荐
深度学习模型剪枝
[sca-cnn interpretation] spatial and channel wise attention
pytorch和tensorflow有什么区别?
How to solve the duplication problem when MySQL inserts data in batches?
Common skills for quantitative investment - drawing 2: drawing the moving average
蓝桥杯单片机第七届决赛
Et5.0 simply transform referencecollectorieditor
Et5.0 configuring Excel
Hard (magnetic) disk (II)
Arduino control tm1637 common positive four digit nixie tube
Set sail
Dynamic planning - good article link
Kotlin coroutine withcontext switch thread
Sequence table - find main element
[JS component] customize the right-click menu
Get preview of precast body
Quantitative investment traditional index investment decision vs Monte Carlo simulation method
[JS component] create a custom horizontal and vertical scroll bar following the steam style
Blinker FAQs
Arduino controls tb6600 driver +42 stepper motor