简体   繁体   中英

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

Any suggestions?

Just initiate your date object with new Date object in your POJO which is mapped in hibernate.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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