为什么要重构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);
}
}

  

  

  

  

扩展JPA方法,重写save方法的更多相关文章

  1. Django model重写save方法及update踩坑记录

    一个非常实用的小方法 试想一下,Django中如果我们想对保存进数据库的数据做校验,有哪些实现的方法? 我们可以在view中去处理,每当view接收请求,就对提交的数据做校验,校验不通过直接返回错误, ...

  2. Java中方法重写和方法重载

     首先方法重写和方法重载是建立在Java的面向对象的继承和多态的特性基础上而出现的.至于面向对象的继承和多态的特性我就不在这里多说了.继承是指在一个父类的基础再创建一个子类,这样子类就拥有了父类的非私 ...

  3. MongoDB中insert方法、update方法、save方法简单对比

    MongoDB中insert方法.update方法.save方法简单对比 1.update方法 该方法用于更新数据,是对文档中的数据进行更新,改变则更新,没改变则不变. 2.insert方法 该方法用 ...

  4. 方法重写和方法重载;this关键字和super关键字

    1:方法重写和方法重载的区别?方法重载能改变返回值类型吗? 方法重写: 在子类中,出现和父类中一模一样的方法声明的现象. 方法重载: 同一个类中,出现的方法名相同,参数列表不同的现象. 方法重载能改变 ...

  5. Java方法重写与方法重载

    方法重载:发生在同一个类中,方法名相同方法形参列表不同就会重载方法. 方法重写:发生在继承当中,如果子的一个类方法与父类中的那个方法一模一样(方法名和形参列表一样),那么子类就会重写父类的方法. 方法 ...

  6. [原创]java WEB学习笔记79:Hibernate学习之路--- 四种对象的状态,session核心方法:save()方法,persist()方法,get() 和 load() 方法,update()方法,saveOrUpdate() 方法,merge() 方法,delete() 方法,evict(),hibernate 调用存储过程,hibernate 与 触发器协同工作

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  7. 【java开发】方法重写和方法重载概述

    类的继承   父类-子类 关键字 extends 新建一个父类 public class Person {     private String name;          private int ...

  8. C++学习笔记24,方法重写与方法隐藏

    该博文仅用于交流学习.请慎用于不论什么商业用途.本博主保留对该博文的一切权利. 博主博客:http://blog.csdn.net/qq844352155 转载请注明出处: 方法重写.是指在子类中又一 ...

  9. Java 深度克隆 clone()方法重写 equals()方法的重写

    1.为什么要重写clone()方法? 答案:Java中的浅度复制是不会把要复制的那个对象的引用对象重新开辟一个新的引用空间,当我们需要深度复制的时候,这个时候我们就要重写clone()方法. 2.为什 ...

随机推荐

  1. h264 aac 封装 flv

    Part 1flvtag组成 FLV 文件结构由 FLVheader和FLVBody组成.(注意flv文件是大端格式的)FLV头组成(以c为例子,一字节对齐):FLVBody是由若干个Tag组成的:  ...

  2. windows平台最简单的rtmp/hls流媒体服务器

    feature: rtmp/hls live server for windows, double click to run,don't need config. run and quit: doub ...

  3. String.Format数字格式化输出 {0:N2} {0:D2} {0:C2} (转)

    String.Format数字格式化输出 {:N2} {:D2} {:C2} (转) //格式为sring输出 // Label1.Text = string.Format("asdfads ...

  4. Docker Toolbox常见错误解决方案

    错误1 Error checking TLS connection: Error checking and/or regenerating the certs: There was an error ...

  5. linux下进程cpu占用过高问题定位方法

    背景 记得前段时间,同事说他们测试环境的服务器cpu使用率一直处于100%,本地又没有什么接口调用,为什么会这样?cpu使用率居高不下,自然是有某些线程一直占用着cpu资源,那又如何查看占用cpu较高 ...

  6. 1094 The Largest Generation

    题意:略. 思路:层序遍历:在结点中增加一个数据域表示结点所在的层次. 代码: #include <cstdio> #include <queue> #include < ...

  7. 【BZOJ】2160: 拉拉队排练(Manacher)

    题目 2160: 拉拉队排练 Description 艾利斯顿商学院篮球队要参加一年一度的市篮球比赛了.拉拉队是篮球比赛的一个看点,好的拉拉队往往能帮助球队增加士气,赢得最终的比赛.所以作为拉拉队队长 ...

  8. 如何实现查询显示N个工作日有效期内的数据

    功能点分析:要显示N个工作日有效期内的数据,需要考虑: 1. 可以给每条数据增加一个有效期时间字段,查询时只显示有效期之前的数据,如有效期为七天,数据的创建时间是2014-07-21,那七个工作日有效 ...

  9. Java线程状态切换以及核心方法

    1.Java线程状态 1.1 线程主要状态 ①初始(NEW):新创建了一个线程对象,但还没有调用start()方法.②运行(RUNNABLE):Java线程中将就绪(ready)和运行中(runnin ...

  10. 从一个开发的角度看负载均衡和LVS--FullNat

    从一个开发的角度看负载均衡和LVS 在大规模互联网应用中,负载均衡设备是必不可少的一个节点,源于互联网应用的高并发和大流量的冲击压力,我们通常会在服务端部署多个无状态的应用服务器和若干有状态的存储服务 ...