jpa中避免save更新时带入null

jpa中只有save方法并没有update方法,那他是怎么实现更新的呢,

首先对于save方法,他会先根据id在数据库里查询,如果没查到数据就会insert一条数据;

如果查到数据,就会update一条数据,但是如果要存入的对象有的字段没有赋值(为null),他就会把null也会插入数据库,那原来这条数据有的字段有值也会被覆盖掉,为了避免这种情况发生,就要做出调整。

一.继承SimpleJpaRepository,改写save方法

package cn.qingsec.test.report.repository;

import cn.qingsec.test.report.utils.BeanUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.support.JpaEntityInformation;
import org.springframework.data.jpa.repository.support.SimpleJpaRepository;

import javax.persistence.EntityManager;
import java.util.Optional;

/**
 * @Author: Liu
 * @Date: 2023/08/30
 * @Description:
 */
@Slf4j
public class ZeusJpaRepository<T, ID> extends SimpleJpaRepository<T, ID> {
    private JpaEntityInformation<T, ?> jpaEntityInformation;

    /**
     * Creates a new {@link ZeusJpaRepository} to manage objects of the given {@link JpaEntityInformation}.
     *
     * @param entityInformation must not be {@literal null}.
     * @param entityManager     must not be {@literal null}.
     */
    @Autowired
    public ZeusJpaRepository(JpaEntityInformation<T, ?> entityInformation, EntityManager entityManager) {
        super(entityInformation, entityManager);
        this.jpaEntityInformation = entityInformation;
    }

    /**
     * 覆盖原来实现,不更新null字段
     * @author 隐
     * @date 2020-03-16
     * @param entity
     * @param <S>
     * @return
     */
    @Override
    public <S extends T> S save(S entity) {
        ID id = (ID) jpaEntityInformation.getId(entity);
        if (id != null) {
            Optional<T> op = findById(id);
            if (op.isPresent()) {
                T t = op.get();
                BeanUtils.copyPropertiesWithoutNull(entity, t);
                entity = (S) t;
            }
        }
        return super.save(entity);
    }
}

二.重写BeanUtils

package cn.qingsec.test.report.utils;

import org.springframework.beans.FatalBeanException;

import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

/**
 * @Author: Liu
 * @Date: 2023/08/30
 * @Description:
 */
public class BeanUtils extends org.springframework.beans.BeanUtils {
    /**
     * bean copy不复制null值
     * @author 隐
     * @date 2018-11-10
     * @param source
     * @param target
     */
    public static void copyPropertiesWithoutNull(Object source, Object target) {
        if(source == null || target == null){
            return;
        }
        Class<?> actualEditable = target.getClass();
        Class<?> sourceClass = source.getClass();
        PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable);
        for (PropertyDescriptor targetPd : targetPds) {
            if(targetPd.getWriteMethod() == null) {
                continue;
            }
            PropertyDescriptor sourcePd = getPropertyDescriptor(sourceClass, targetPd.getName());
            if(sourcePd == null || sourcePd.getReadMethod() == null) {
                continue;
            }
            try {
                Method readMethod = sourcePd.getReadMethod();
                if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
                    readMethod.setAccessible(true);
                }
                Object value = readMethod.invoke(source);
                setValue(target, targetPd, value);
            } catch (Exception ex) {
                throw new FatalBeanException("Could not copy properties from source to target", ex);
            }
        }
    }

    /**
     * 设置值到目标bean
     * @param target
     * @param targetPd
     * @param value
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     */
    private static void setValue(Object target, PropertyDescriptor targetPd, Object value) throws IllegalAccessException, InvocationTargetException {
        // 这里判断以下value是否为空
        if (value != null) {
            Method writeMethod = targetPd.getWriteMethod();
            if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
                writeMethod.setAccessible(true);
            }
            writeMethod.invoke(target, value);
        }
    }
}

三.在启动类上加注解

@EnableJpaRepositories(repositoryBaseClass = ZeusJpaRepository.class)

四.加上注解

在实体类上加上注解

@DynamicUpdate
@DynamicInsert
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在使用JPA的save方法,如果直接在查出的实体上设置某字段为null并保存,由于session缓存的原因,仍然会将null字段保存到数据库。这可能会覆盖数据库原有的数据,这是一个很可怕的行为。为了解决这个问题,有几种方法可以尝试: 1. 在实体类上添加@DynamicInsert(true)/@DynamicUpdate(true)注解。这样会在执行保存操作,只会将非null字段保存到数据库null字段将被忽略。 2. 可以扩展SimpleJpaRepository,并覆盖save方法,在保存实体前对null字段进行处理,使其不保存到数据库。 3. 在启动类上添加@EnableJpaRepositories(repositoryBaseClass = ZeusJpaRepository.class)注解,将自定义的JpaRepository作为基础类,然后在自定义的JpaRepository处理null字段的保存逻辑。 这些方法可以帮助你在使用JPA的save方法避免保存null值到数据库。请根据你的具体需求选择合适的方法来解决这个问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [JPA踩坑记:Spring Data Jpa 更新null的问题(save方法保存null值会被更新到数据库)](https://blog.csdn.net/adu003/article/details/105294362)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lyt5701

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值