为什么我用@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;//类目id

    private 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回答

廖师兄

2019-08-30

这条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`)
);


0
4
qq_慕前端2496093
非常感谢!
2019-09-02
共4条回复

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



0
0

廖师兄

2019-08-29

看控制台的sql语句

0
1
qq_慕前端2496093
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
2019-08-30
共1条回复

Spring Boot双版本(1.5/2.1) 打造企业级微信点餐系统

从0到1开发中小型企业级Java应用,并学会迭代重构技巧

6410 学习 · 5247 问题

查看课程