为什么我用@DynamicUpdate无法更新时间,不写private Date createTime,updateTime 就可以
来源:4-2 买家类目-dao(下)
qq_慕前端2496093
2019-08-28
debug调式的截图
实体类:
package com.imooc.dataobject;
import lombok.Data;
import org.hibernate.annotations.DynamicUpdate;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import java.util.Date;
/**
-
类目
*/
@Entity
@DynamicUpdate
@Data
public class ProductCategory {
@Id//主键
@GeneratedValue//自增
private Integer categoryId;//类目idprivate String categoryName;//类目名字
private Integer categoryType;//类目编号
private Date createTime;
private Date updateTime;
public ProductCategory() {
}public ProductCategory(String categoryName, Integer categoryType) {
this.categoryName = categoryName;
this.categoryType = categoryType;
}
}
将 private Date createTime; private Date updateTime;去掉就可以添加时间。
3回答
-
这条sql,是没加@DynamicUpdate的时候打印的吧
insert into product_category (category_name, category_type, create_time, update_time) values (?, ?, ?, ?)
要加上@DynamicUpdate的。
再看看表结构,和我的不一样吗?
-- 类目create table `product_category` ( `category_id` int not null auto_increment, `category_name` varchar(64) not null comment '类目名字', `category_type` int not null comment '类目编号', `create_time` timestamp not null default current_timestamp comment '创建时间', `update_time` timestamp not null default current_timestamp on update current_timestamp comment '修改时间', primary key (`category_id`), UNIQUE KEY `uqe_category_type` (`category_type`) );
042019-09-02 -
qq_慕前端2496093
提问者
2019-08-30
Hibernate: insert into product_category (category_name, category_type, create_time, update_time) values (?, ?, ?, ?)
2019-08-30 14:22:09,874 - binding parameter [1] as [VARCHAR] - [呵呵呵]
2019-08-30 14:22:09,875 - binding parameter [2] as [INTEGER] - [6]
2019-08-30 14:22:09,876 - binding parameter [3] as [TIMESTAMP] - [null]
2019-08-30 14:22:09,876 - binding parameter [4] as [TIMESTAMP] - [null]
2019-08-30 14:22:09,890 - SQL Error: 1048, SQLState: 23000
2019-08-30 14:22:09,890 - Column 'create_time' cannot be null
org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
00 -
廖师兄
2019-08-29
看控制台的sql语句
012019-08-30
相似问题