@dynamicupdate //自动更新updatetime
在数据库中的字节,包括updatetime,但是我更新某一个内容时,updatetime,没有自动更新,这时候我们只需要在data类中加上注解 @dynamicupdate 动态更新的意思
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
@entity @dynamicupdate //自动更新(动态更新)updatetime public class productcategory { /*类目id*/ @id //主键 @generatedvalue(strategy = generationtype.identity) //子增类型 private integer categoryid; /*类目名字*/ private string categoryname; /*类目编号*/ private integer categorytype;
/*创建时间*/ private date createtime; /*更新时间*/ private date updatetime;
public integer getcategoryid( int i) { return categoryid; }
public void setcategoryid(integer categoryid) { this .categoryid = categoryid; }
public string getcategoryname() { return categoryname; }
public void setcategoryname(string categoryname) { this .categoryname = categoryname; }
public integer getcategorytype() { return categorytype; }
public void setcategorytype(integer categorytype) { this .categorytype = categorytype; }
public date getcreatetime() { return createtime; }
public void setcreatetime(date createtime) { this .createtime = createtime; }
public date getupdatetime() { return updatetime; }
public void setupdatetime(date updatetime) { this .updatetime = updatetime; }
@override public string tostring() { return "productcategory{" + "categoryid=" + categoryid + ", categoryname='" + categoryname + '\ '' + ", categorytype=" + categorytype + '}' ; }
} |
@dynamicupdate 注解使用及注意事项
使用场景
平时在写业务时, 会涉及到某条数据的更新。 当我们使用hibernate的 this.getcurrentsession().saveorupdate(o) 更新对象时,会默认的更新对象(o)所有的字段,包括属性为null和未修改的字段也会更新到原有的数据库表中。
造成了原有的数据丢失或数据重复修改。
通常这情况下我们所希望的是仅更新对象(o)中修改过且有值的字段,此时就需要用到@dynamicupdate注解。
注解使用
标注位置: 实体映射类上
注意事项
根据官方接口文档所说,如果我们在使用该注解时必须同时使用 @selectbeforeupdate 注解表明在更新前先进行查询操作。否则是即使声明该注解也是无效的。
个人理解:
也就是说当我们要更新的对象不在session会话的管理中,无法比对哪个字段需要更新,则所标注的注解无效。故需要在其更新前进行查询,此时当前数据已经在sessio的会话中,即可动态更新数据。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。
原文链接:https://blog.csdn.net/chaoge321/article/details/83313122
查看更多关于@DynamicUpdate //自动更新updatetime的问题的详细内容...