关于更新当前时间戳的Mysql 5.6列在hibernate中不起作用(Mysql 5.6 column on update current timestamp not working in hibe

编程入门 行业动态 更新时间:2024-10-25 02:28:37
关于更新当前时间戳的Mysql 5.6列在hibernate中不起作用(Mysql 5.6 column on update current timestamp not working in hibernate)

我在表格答案中有一个名为lastModified的列,如下所示

+--------------+----------------+------+-----+-------------------+-----------------------------+ | Field | Type | Null | Key | Default | Extra | +--------------+----------------+------+-----+-------------------+-----------------------------+ | answerId | int(11) | NO | PRI | NULL | auto_increment | | totalComment | int(11) | NO | | 0 | | | totalView | int(11) | NO | | 0 | | | totalSpam | int(11) | YES | | 0 | | | lastModified | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP | +--------------+----------------+------+-----+-------------------+-----------------------------+

每当我更新到totalComment中的任何一个,mysql控制台中的totalView,totalSpam列时,lastModifed列也会被当前时间戳修改。 但是,当我使用hibernate应用程序做同样的事情时,它没有得到修改。

我的应用程序是作为spring数据jpa的一部分实现的。 在application.yml中,我尝试使用org.hibernate.dialect.MySQLDialectorg.hibernate.dialect.MySQL5Dialect但没有帮助。

在我的带注释的Answer实体类中,lastModified列声明如下

@Column private Date lastModified; public Date getLastModified() { return lastModified; } public void setLastModified(Date lastModified) { this.lastModified = lastModified; }

任何想法 ?

I have a column called lastModified in the table answer, something like following

+--------------+----------------+------+-----+-------------------+-----------------------------+ | Field | Type | Null | Key | Default | Extra | +--------------+----------------+------+-----+-------------------+-----------------------------+ | answerId | int(11) | NO | PRI | NULL | auto_increment | | totalComment | int(11) | NO | | 0 | | | totalView | int(11) | NO | | 0 | | | totalSpam | int(11) | YES | | 0 | | | lastModified | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP | +--------------+----------------+------+-----+-------------------+-----------------------------+

Whenever I manullay update to any one of totalComment, totalView, totalSpam columns in mysql console the lastModifed column also get modified with current timestamp. But when I am doing the same thing with hibernate application it is not getting modified.

My application is implemented as part of spring data jpa. In application.yml I tried using both org.hibernate.dialect.MySQLDialect and org.hibernate.dialect.MySQL5Dialect but no help.

In my annotated Answer entity class lastModified column is declared as following

@Column private Date lastModified; public Date getLastModified() { return lastModified; } public void setLastModified(Date lastModified) { this.lastModified = lastModified; }

Any Idea ?

最满意答案

正如@Shadow所提到的,您当前的架构设计意味着您希望MySQL为您自动设置lastModified字段( on update CURRENT_TIMESTAMP )。 因此,如果从Java代码传递null ,那么它应该按预期工作。 尝试在列定义中使用updatable = false选项告诉JPA不要向MySQL发送值:

@Column(name = "lastModified", updatable = false) private Date lastModified;

我猜测当前发生的事情是Hibernate使用数据库中的值填充lastModified字段,然后你的JPA代码将这个相同的值保存回MySQL。 所以似乎没有更新该值。 但真正发生的是你的JPA代码正在使用相同的旧值进行更新。

As the @Shadow mentioned, your current schema design implies that you want MySQL to automatically set the lastModified field for you (on update CURRENT_TIMESTAMP). So if you pass null from your Java code, then it should work as expected. Try using the updatable = false option in your column definition to tell JPA not to send a value to MySQL:

@Column(name = "lastModified", updatable = false) private Date lastModified;

My guess as to what is currently happening is that Hibernate is populating the lastModified field with the value from the database, and then your JPA code is then persisting this same value back to MySQL. So it appears that the value is not being updated. But what is really happening is that your JPA code is doing an update with the same old value.

更多推荐

本文发布于:2023-07-24 20:11:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1250583.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中不   时间   hibernate   working   Mysql

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!