我在表格答案中有一个名为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.MySQLDialect和org.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.
更多推荐
发布评论