简体   繁体   English

Hibernate将默认日期时间插入不为空的列

[英]Hibernate insert default datetime to a not null column

In my table I have a column with following definition. 在我的表格中,我有一列具有以下定义。

createdat DATETIME NOT NULL DEFAULT NOW(),

I'm trying to insert data in to this table using hibernate. 我正在尝试使用休眠将数据插入此表中。 I want this column to have default value. 我希望此列具有默认值。 But it gives me exception 但这给了我例外

com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'createdat' cannot be null com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:列'createdat'不能为null

Any suggestions? 有什么建议么?

Just initiate your date object with new Date object in your POJO which is mapped in hibernate. 只需在您的POJO中用休眠中映射的新Date对象初始化date对象。

java.util.Date date = new java.util.Date();

I suppose you have your setters and getter for POJO, whenever you forgot or dont want to set date object this default date will be used 我想您有用于POJO的设置器和获取器,每当您忘记或不想设置日期对象时,都将使用该默认日期

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM