29-异常-DataIntegrityViolationException
更多内容欢迎关注我(持续更新中,欢迎Star✨)
Github:CodeZeng1998/Java-Developer-Work-Note
(技术)微信公众号:CodeZeng1998
(生活)微信公众号:好锅
其他平台:CodeZeng1998、好锅
问题描述
插入数据出现如下报错。DataIntegrityViolationException 英文直译过来的意思就是:数据完整性违反异常
报错信息
org.springframework.dao.DataIntegrityViolationException:
### Error updating database. Cause: java.sql.SQLException: Field 'xxx' doesn't have a default value
### The error may exist in com/xxx/xxx/xxx/xxx/xxx/dao/XxxMapper.java (best guess)
### The error may involve com.xxx.xxx.xxx.xxx.xxx.dao.XxxyMapper.insert
### The error occurred while executing an update
### SQL: INSERT INTO `xxx_table`(`id`, `created_time`, `updated_time`, `deleted`) VALUES (?, ?, ?, ?)
### Cause: java.sql.SQLException: Field 'xxx' doesn't have a default value
; Field 'xxx' doesn't have a default value
Caused by: java.sql.SQLException: Field 'xxx' doesn't have a default value
... 135 common frames omitted
错误原因
数据库设置好了对应的字段的值不能为空。
解决方案
- 方案一:插入数据的时候带上对应字段的值
INSERT INTO `xxx_table`(`id`, `created_time`, `updated_time`, `deleted`, `xxx`) VALUES (?, ?, ?, ?, ?)
- 方案二:数据库对应的字段允许为Null值
- 方案三:给数据库对应的字段设置好对应的默认值
更多内容欢迎关注我(持续更新中,欢迎Star✨)
Github:CodeZeng1998/Java-Developer-Work-Note
(技术)微信公众号:CodeZeng1998
(生活)微信公众号:好锅
其他平台:CodeZeng1998、好锅