使用jpa之动态插入与修改(重写save)


Posted in Java/Android onNovember 23, 2021

jpa之动态插入与修改(重写save)

1.动态插入

@Data
@Entity
@DynamicInsert
@Table(name = "cpu_dynamics_information")
@EntityListeners(AuditingEntityListener.class)
public class CpuDynamicsInformation extends CommonEntity implements Serializable {
  private static final long serialVersionUID = -662804563658253624L;
  // cpu动态属性
  private Integer cpuCore;
  // cpu用户使用率
  private Double cpuUseRate;
  // cpu系统使用率
  private Double cpuSysRate;
  // cpu等待率
  private Double cpuWaitRate;
  // cpu空闲率
  private Double cpuIdleRate;
  // cpu总的使用率
  private Double cpuCombineRate;
  private Long serverId;
}

关键注解:

@DynamicInsert
@EntityListeners(AuditingEntityListener.class)

2.重写save(修改)

@SuppressWarnings(value = "all")
public class JpaRepositoryReBuild<T, ID> extends SimpleJpaRepository<T, ID> {
  private final JpaEntityInformation<T, ?> entityInformation;
  private final EntityManager em;
  @Autowired
  public JpaRepositoryReBuild(
      JpaEntityInformation<T, ?> entityInformation, EntityManager entityManager) {
    super(entityInformation, entityManager);
    this.entityInformation = entityInformation;
    this.em = entityManager;
  }
  /** 通用save方法 :新增/选择性更新 */
  @Override
  @Transactional
  public <S extends T> S save(S entity) {
    // 获取ID
    ID entityId = (ID) this.entityInformation.getId(entity);
    T managedEntity;
    T mergedEntity;
    if (entityId == null) {
      em.persist(entity);
      mergedEntity = entity;
    } else {
      managedEntity = this.findById(entityId).get();
      if (managedEntity == null) {
        em.persist(entity);
        mergedEntity = entity;
      } else {
        BeanUtils.copyProperties(entity, managedEntity, getNullProperties(entity));
        em.merge(managedEntity);
        mergedEntity = managedEntity;
      }
    }
    return entity;
  }
  /** 获取对象的空属性 */
  private static String[] getNullProperties(Object src) {
    // 1.获取Bean
    BeanWrapper srcBean = new BeanWrapperImpl(src);
    // 2.获取Bean的属性描述
    PropertyDescriptor[] pds = srcBean.getPropertyDescriptors();
    // 3.获取Bean的空属性
    Set<String> properties = new HashSet<>();
    for (PropertyDescriptor propertyDescriptor : pds) {
      String propertyName = propertyDescriptor.getName();
      Object propertyValue = srcBean.getPropertyValue(propertyName);
      if (StringUtils.isEmpty(propertyValue)) {
        srcBean.setPropertyValue(propertyName, null);
        properties.add(propertyName);
      }
    }
    return properties.toArray(new String[0]);
  }
}

3.启动类

@EnableJpaAuditing
@SpringBootApplication(exclude = MongoAutoConfiguration.class)
@EnableJpaRepositories(
    value = {"com.fooww.research.repository", "com.fooww.research.shiro.repository"},
    repositoryBaseClass = JpaRepositoryReBuild.class)
public class MonitorServerApplication {
  public static void main(String[] args) {
    SpringApplication.run(MonitorServerApplication.class, args);
  }
}

关键注释:

  • EnableJpaRepositories 扫描的repository包
  • repositoryBaseClass 重写的save类
  • EnableJpaAuditing 使@EntityListeners(AuditingEntityListener.class) 生效

扩展JPA方法,重写save方法

为什么要重构save?

jpa提供的save方法会将原有数据置为null,而大多数情况下我们只希望跟新自己传入的参数,所以便有了重写或者新增一个save方法。

本着解决这个问题,网上搜了很多解决方案,但是没有找到合适的,于是自己研究源码,先展示几个重要源码

1、SimpleJpaRepository方法实现类,由于代码过多只展示部分源码

public class SimpleJpaRepository<T, ID> implements JpaRepository<T, ID>, JpaSpecificationExecutor<T> {
    private static final String ID_MUST_NOT_BE_NULL = "The given id must not be null!";
    private final JpaEntityInformation<T, ?> entityInformation;
    private final EntityManager em;
    private final PersistenceProvider provider;
    @Nullable
    private CrudMethodMetadata metadata;
 
    public SimpleJpaRepository(JpaEntityInformation<T, ?> entityInformation, EntityManager entityManager) {
        Assert.notNull(entityInformation, "JpaEntityInformation must not be null!");
        Assert.notNull(entityManager, "EntityManager must not be null!");
        this.entityInformation = entityInformation;
        this.em = entityManager;
        this.provider = PersistenceProvider.fromEntityManager(entityManager);
    }
 
    public SimpleJpaRepository(Class<T> domainClass, EntityManager em) {
        this(JpaEntityInformationSupport.getEntityInformation(domainClass, em), em);
    }
 
    public void setRepositoryMethodMetadata(CrudMethodMetadata crudMethodMetadata) {
        this.metadata = crudMethodMetadata;
    }
 
    @Nullable
    protected CrudMethodMetadata getRepositoryMethodMetadata() {
        return this.metadata;
    }
 
    protected Class<T> getDomainClass() {
        return this.entityInformation.getJavaType();
    }
 
    private String getDeleteAllQueryString() {
        return QueryUtils.getQueryString("delete from %s x", this.entityInformation.getEntityName());
    }
    @Transactional
    public <S extends T> S save(S entity) {
        if (this.entityInformation.isNew(entity)) {
            this.em.persist(entity);
            return entity;
        } else {
            return this.em.merge(entity);
        }
    }
}

2、JpaRepositoryFactoryBean

public class JpaRepositoryFactoryBean<T extends Repository<S, ID>, S, ID> extends TransactionalRepositoryFactoryBeanSupport<T, S, ID> {
    @Nullable
    private EntityManager entityManager; 
    public JpaRepositoryFactoryBean(Class<? extends T> repositoryInterface) {
        super(repositoryInterface);
    }
 
    @PersistenceContext
    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }
 
    public void setMappingContext(MappingContext<?, ?> mappingContext) {
        super.setMappingContext(mappingContext);
    }
 
    protected RepositoryFactorySupport doCreateRepositoryFactory() {
        Assert.state(this.entityManager != null, "EntityManager must not be null!");
        return this.createRepositoryFactory(this.entityManager);
    }
 
    protected RepositoryFactorySupport createRepositoryFactory(EntityManager entityManager) {
        return new JpaRepositoryFactory(entityManager);
    }
 
    public void afterPropertiesSet() {
        Assert.state(this.entityManager != null, "EntityManager must not be null!");
        super.afterPropertiesSet();
    }
} 

根据源码及网上资料总结如下方案

一、重写save

优势:侵入性小,缺点将原方法覆盖。

创建JpaRepositoryReBuild方法继承SimpleJpaRepository。

直接上代码

public class JpaRepositoryReBuild<T, ID> extends SimpleJpaRepository<T, ID> { 
    private final JpaEntityInformation<T, ?> entityInformation;
    private final EntityManager em; 
    @Autowired
    public JpaRepositoryReBuild(JpaEntityInformation<T, ?> entityInformation, EntityManager entityManager) {
        super(entityInformation, entityManager);
        this.entityInformation = entityInformation;
        this.em = entityManager;
    }
 
    /**
     * 通用save方法 :新增/选择性更新
     */
    @Override
    @Transactional
    public <S extends T> S save(S entity) {
         
        //获取ID
        ID entityId = (ID) this.entityInformation.getId(entity);
        T managedEntity;
        T mergedEntity;
        if(entityId == null){
            em.persist(entity);
            mergedEntity = entity;
        }else{
            managedEntity = this.findById(entityId).get();
            if (managedEntity == null) {
                em.persist(entity);
                mergedEntity = entity;
            } else {
                BeanUtils.copyProperties(entity, managedEntity, getNullProperties(entity));
                em.merge(managedEntity);
                mergedEntity = managedEntity;
            }
        }
        return entity;
    }
 
    /**
     * 获取对象的空属性
     */
    private static String[] getNullProperties(Object src) {
        //1.获取Bean
        BeanWrapper srcBean = new BeanWrapperImpl(src);
        //2.获取Bean的属性描述
        PropertyDescriptor[] pds = srcBean.getPropertyDescriptors();
        //3.获取Bean的空属性
        Set<String> properties = new HashSet<>();
        for (PropertyDescriptor propertyDescriptor : pds) {
            String propertyName = propertyDescriptor.getName();
            Object propertyValue = srcBean.getPropertyValue(propertyName);
            if (StringUtils.isEmpty(propertyValue)) {
                srcBean.setPropertyValue(propertyName, null);
                properties.add(propertyName);
            }
        }
        return properties.toArray(new String[0]);
    }
}

启动类加上JpaRepositoryReBuild 方法

@EnableJpaRepositories(value = "com.XXX", repositoryBaseClass = JpaRepositoryReBuild.class)
@SpringBootApplication
@EnableDiscoveryClient // 即消费也注册
public class SystemApplication { 
    public static void main(String[] args) {
        SpringApplication.run(SystemApplication.class, args);
    }     
}

二、扩张jpa方法

1、新建新增方法接口BaseRepository

@NoRepositoryBean
public interface BaseRepository<T, ID extends Serializable> extends JpaRepository<T, ID> { 
    /**
     * 保存但不覆盖原有数据
     * @param entity
     * @return
     */
    T saveNotNull(T entity);
}

2、创建BaseRepositoryImpl方法

@NoRepositoryBean
public class BaseRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRepository<T, ID> implements BaseRepository<T, ID> {  
    private final JpaEntityInformation<T, ?> entityInformation;
    private final EntityManager em;   
    public BaseRepositoryImpl(JpaEntityInformation<T, ?> entityInformation, EntityManager entityManager) {
        super(entityInformation,entityManager);
        this.entityInformation = entityInformation;
        this.em = entityManager;
    }
 
    public BaseRepositoryImpl(Class<T> domainClass, EntityManager em) {
        this(JpaEntityInformationSupport.getEntityInformation(domainClass, em), em);
    }
 
    @Override
    @Transactional
    public T saveNotNull(T entity) { 
        //获取ID
        ID entityId = (ID) this.entityInformation.getId(entity);
        T managedEntity;
        T mergedEntity;
        if(entityId == null){
            em.persist(entity);
            mergedEntity = entity;
        }else{
            managedEntity = this.findById(entityId).get();
            if (managedEntity == null) {
                em.persist(entity);
                mergedEntity = entity;
            } else {
                BeanUtils.copyProperties(entity, managedEntity, getNullProperties(entity));
                em.merge(managedEntity);
                mergedEntity = managedEntity;
            }
        }
        return mergedEntity;
    } 
 
    private static String[] getNullProperties(Object src) {
        //1.获取Bean
        BeanWrapper srcBean = new BeanWrapperImpl(src);
        //2.获取Bean的属性描述
        PropertyDescriptor[] pds = srcBean.getPropertyDescriptors();
        //3.获取Bean的空属性
        Set<String> properties = new HashSet<>();
        for (PropertyDescriptor propertyDescriptor : pds) {
            String propertyName = propertyDescriptor.getName();
            Object propertyValue = srcBean.getPropertyValue(propertyName);
            if (StringUtils.isEmpty(propertyValue)) {
                srcBean.setPropertyValue(propertyName, null);
                properties.add(propertyName);
            }
        }
        return properties.toArray(new String[0]);
    }
}

3、创建工厂BaseRepositoryFactory

public class BaseRepositoryFactory<R extends JpaRepository<T, ID>, T, ID extends Serializable> extends JpaRepositoryFactoryBean<R, T, ID> { 
    public BaseRepositoryFactory(Class<? extends R> repositoryInterface) {
        super(repositoryInterface);
    }
 
    @Override
    protected RepositoryFactorySupport createRepositoryFactory(EntityManager em) {
        return new MyRepositoryFactory(em);
    }
 
    private static class MyRepositoryFactory extends JpaRepositoryFactory { 
        private final EntityManager em;
        public MyRepositoryFactory(EntityManager em) {
            super(em);
            this.em = em;
        }
 
        @Override
        protected Object getTargetRepository(RepositoryInformation information) {
            return new BaseRepositoryImpl((Class) information.getDomainType(), em);
        }
 
        @Override
        protected Class getRepositoryBaseClass(RepositoryMetadata metadata) {
            return BaseRepositoryImpl.class;
        } 
    } 
}

4、启动类引入

@EnableJpaRepositories(repositoryFactoryBeanClass = BaseRepositoryFactory.class, basePackages ="com.XXX")
@SpringBootApplication
@EnableDiscoveryClient // 即消费也注册
public class SystemApplication { 
    public static void main(String[] args) {
        SpringApplication.run(SystemApplication.class, args);
    }
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Java/Android 相关文章推荐
教你用Java在个人电脑上实现微信扫码支付
Jun 13 Java/Android
一篇带你入门Java垃圾回收器
Jun 16 Java/Android
Java并发编程必备之Future机制
Jun 30 Java/Android
java基础——多线程
Jul 03 Java/Android
swagger如何返回map字段注释
Jul 03 Java/Android
浅谈Java父子类加载顺序
Aug 04 Java/Android
springmvc直接不经过controller访问WEB-INF中的页面问题
Feb 24 Java/Android
关于Mybatis中SQL节点的深入解析
Mar 19 Java/Android
详解Flutter和Dart取消Future的三种方法
Apr 07 Java/Android
Elasticsearch Recovery 详细介绍
Apr 19 Java/Android
Spring Boot优化后启动速度快到飞起技巧示例
Jul 23 Java/Android
Java实现贪吃蛇游戏的示例代码
Sep 23 Java/Android
Jpa Specification如何实现and和or同时使用查询
Nov 23 #Java/Android
解析mybatis-plus中的resultMap简单使用
Nov 23 #Java/Android
JPA 通过Specification如何实现复杂查询
Java使用JMeter进行高并发测试
Java 在线考试云平台的实现
OpenCV实现反阈值二值化
聊聊SpringBoot自动装配的魔力
Nov 17 #Java/Android
You might like
PHP 代码规范小结
2012/03/08 PHP
浅析51个PHP处理字符串的函数
2013/08/02 PHP
thinkPHP js文件中U方法不被解析问题的解决方法
2016/12/05 PHP
JavaScript 异步调用框架 (Part 1 - 问题 &amp; 场景)
2009/08/03 Javascript
用jQuery打造TabPanel效果代码
2010/05/22 Javascript
javascript 获取函数形参个数
2014/07/31 Javascript
JS辨别访问浏览器判断是android还是ios系统
2014/08/19 Javascript
JavaScript跨域方法汇总
2014/10/16 Javascript
javascript实现状态栏中文字动态显示的方法
2015/10/20 Javascript
js实现iframe框架取值的方法(兼容IE,firefox,chrome等)
2015/11/26 Javascript
jquery实现无刷新验证码的简单实例
2016/05/19 Javascript
AngularJS指令中的绑定策略实例分析
2016/12/14 Javascript
详解nodejs 文本操作模块-fs模块(四)
2016/12/22 NodeJs
BootStrap 动态表单效果
2017/06/02 Javascript
JavaScript实现HTML5游戏断线自动重连的方法
2017/09/18 Javascript
微信小程序 上传头像的实例详解
2017/10/27 Javascript
vue中的scope使用详解
2017/10/29 Javascript
JavaScript实现QQ列表展开收缩扩展功能
2017/10/30 Javascript
基于element-ui的rules中正则表达式
2018/09/04 Javascript
关于Vue Router中路由守卫的应用及在全局导航守卫中检查元字段的方法
2018/12/09 Javascript
使用layui+ajax实现简单的菜单权限管理及排序的方法
2019/09/10 Javascript
Python学习教程之常用的内置函数大全
2017/07/14 Python
更新修改后的Python模块方法
2019/03/03 Python
Python XlsxWriter模块Chart类用法实例分析
2019/03/11 Python
Django REST framework 分页的实现代码
2019/06/19 Python
详解基于Jupyter notebooks采用sklearn库实现多元回归方程编程
2020/03/25 Python
html5 offlline 缓存使用示例
2013/06/24 HTML / CSS
html5 viewport使用方法示例详解
2013/12/02 HTML / CSS
资生堂英国官网:Shiseido英国
2020/12/30 全球购物
创建索引时需要注意的事项
2013/05/13 面试题
初中化学教学反思
2014/01/23 职场文书
公务员个人年终总结
2015/02/12 职场文书
升学宴学生致辞
2015/09/29 职场文书
关于React Native使用axios进行网络请求的方法
2021/08/02 Javascript
gojs实现蚂蚁线动画效果
2022/02/18 Javascript
Vue ECharts实现机舱座位选择展示功能
2022/05/15 Vue.js