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);
}
}
附件中贴出代码包