DynamicUpdate注解

DynamicUpdate注解

DynamicUpdate注解的作用

mysql中有一个字段 updatetime

想每次操作更改数据表的时候更改update字段

首先修改该字段的属性
Alter Table product_category MODIFY COLUMN update_time TIMESTAMP NOT null DEFAULT CURRENT_TIMESTAMP
on UPDATE CURRENT_TIMESTAMP

在实体类在加入@updatetime注解

在测试单元中添加对字段的修改,这个自动一定要修改过,不然时间不会更改;

代码:
测试方法

 @Test
    public void insterProduct(){
        Optional<ProductCategory> productCategory=productCategoryRepository.findById(1);
        ProductCategory product=productCategory.get();
        System.out.println("************"+product.toString());
        product.setCategoryType(10);
        productCategoryRepository.save(product);

        log信息:Hibernate: update product_category set category_type=? where category_id=?
     
        需要注意 DynamicUpdate注解默认为true
     
        源码:
     
        @Target({ElementType.TYPE})
       @Retention(RetentionPolicy.RUNTIME)
        public @interface DynamicUpdate {
    boolean value() default true;

}

​ 当把属性值更改为false时
​ log信息:Hibernate Hibernate: select productcat0_.category_id as category1_0_0_, productcat0_.category_name as category2_0_0_, productcat0_.category_type as category3_0_0_, productcat0_.create_time as create_t4_0_0_, productcat0_.update_time as update_t5_0_0_ from product_category productcat0_ where productcat0_.category_id=?
​ 此时只修改mysql字段为非空的属性,自动更新的字段updatetime不会更新

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值