spring data jpa 动态更新@DynamicUpdate

 

import org.springframework.beans.BeanUtils;


    @Transactional
    public  User update(User record){
        User old=this.findById(record.getId());
        record.preUpdate();
        BeanUtils.copyProperties(record, old, BeanUtilsExt.getNullPropertyNames(record));
        return   userDAO.save(old);
    }

 

public class BeanUtilsExt {


    public static String[] getNullPropertyNames (Object source) {
        final BeanWrapper src = new BeanWrapperImpl(source);
        java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();

        Set<String> emptyNames = new HashSet<String>();
        for(java.beans.PropertyDescriptor pd : pds) {
            Object srcValue = src.getPropertyValue(pd.getName());
            if (srcValue == null) emptyNames.add(pd.getName());
        }
        String[] result = new String[emptyNames.size()];
        return emptyNames.toArray(result);
    }
}

 以方法可以实现动态更新

 

Hibernate: select sendtaskhi0_.id as id1_1_0_, sendtaskhi0_.create_date as create_d2_1_0_, sendtaskhi0_.create_user as create_u3_1_0_, sendtaskhi0_.update_date as update_d4_1_0_, sendtaskhi0_.credit_id as credit_i5_1_0_, sendtaskhi0_.cus_mobile as cus_mobi6_1_0_, sendtaskhi0_.cus_name as cus_name7_1_0_, sendtaskhi0_.g_id as g_id8_1_0_, sendtaskhi0_.id_card as id_card9_1_0_, sendtaskhi0_.list_id as list_id10_1_0_, sendtaskhi0_.response_msg as respons11_1_0_, sendtaskhi0_.request_id as request12_1_0_, sendtaskhi0_.sub_merch as sub_mer13_1_0_, sendtaskhi0_.task_content as task_co14_1_0_, sendtaskhi0_.task_g_date as task_g_15_1_0_, sendtaskhi0_.task_his_id as task_hi16_1_0_, sendtaskhi0_.task_num as task_nu17_1_0_, sendtaskhi0_.task_r as task_r18_1_0_, sendtaskhi0_.task_s_date as task_s_19_1_0_, sendtaskhi0_.task_status as task_st20_1_0_, sendtaskhi0_.task_type as task_ty21_1_0_, sendtaskhi0_.tts as tts22_1_0_ from c_send_task_his sendtaskhi0_ where sendtaskhi0_.id=?
Hibernate: select sendtaskhi0_.id as id1_1_0_, sendtaskhi0_.create_date as create_d2_1_0_, sendtaskhi0_.create_user as create_u3_1_0_, sendtaskhi0_.update_date as update_d4_1_0_, sendtaskhi0_.credit_id as credit_i5_1_0_, sendtaskhi0_.cus_mobile as cus_mobi6_1_0_, sendtaskhi0_.cus_name as cus_name7_1_0_, sendtaskhi0_.g_id as g_id8_1_0_, sendtaskhi0_.id_card as id_card9_1_0_, sendtaskhi0_.list_id as list_id10_1_0_, sendtaskhi0_.response_msg as respons11_1_0_, sendtaskhi0_.request_id as request12_1_0_, sendtaskhi0_.sub_merch as sub_mer13_1_0_, sendtaskhi0_.task_content as task_co14_1_0_, sendtaskhi0_.task_g_date as task_g_15_1_0_, sendtaskhi0_.task_his_id as task_hi16_1_0_, sendtaskhi0_.task_num as task_nu17_1_0_, sendtaskhi0_.task_r as task_r18_1_0_, sendtaskhi0_.task_s_date as task_s_19_1_0_, sendtaskhi0_.task_status as task_st20_1_0_, sendtaskhi0_.task_type as task_ty21_1_0_, sendtaskhi0_.tts as tts22_1_0_ from c_send_task_his sendtaskhi0_ where sendtaskhi0_.id=?
Hibernate: update c_send_task_his set update_date=?, cus_mobile=?, cus_name=? where id=?
Hibernate: select sendtaskhi0_.id as id1_1_, sendtaskhi0_.create_date as create_d2_1_, sendtaskhi0_.create_user as create_u3_1_, sendtaskhi0_.update_date as update_d4_1_, sendtaskhi0_.credit_id as credit_i5_1_, sendtaskhi0_.cus_mobile as cus_mobi6_1_, sendtaskhi0_.cus_name as cus_name7_1_, sendtaskhi0_.g_id as g_id8_1_, sendtaskhi0_.id_card as id_card9_1_, sendtaskhi0_.list_id as list_id10_1_, sendtaskhi0_.response_msg as respons11_1_, sendtaskhi0_.request_id as request12_1_, sendtaskhi0_.sub_merch as sub_mer13_1_, sendtaskhi0_.task_content as task_co14_1_, sendtaskhi0_.task_g_date as task_g_15_1_, sendtaskhi0_.task_his_id as task_hi16_1_, sendtaskhi0_.task_num as task_nu17_1_, sendtaskhi0_.task_r as task_r18_1_, sendtaskhi0_.task_s_date as task_s_19_1_, sendtaskhi0_.task_status as task_st20_1_, sendtaskhi0_.task_type as task_ty21_1_, sendtaskhi0_.tts as tts22_1_ from c_send_task_his sendtaskhi0_ order by sendtaskhi0_.id desc limit ?
Hibernate: select count(sendtaskhi0_.id) as col_0_0_ from c_send_task_his sendtaskhi0_

 第二场景下是spring boot,但没试验成功,这里也贴出来

@SpringBootApplication
@EnableJpaRepositories(repositoryFactoryBeanClass = ExtJpaRepositoryFactoryBean.class)
public class CallerCentApplication {

	public static void main(String[] args) {

		SpringApplication springApplication =new SpringApplication(CallerCentApplication.class);
		//springApplication.addListeners(new ApplicationEventListener());
		springApplication.run(args);
	}
}

 附件中贴出代码包

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值