错误信息:
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement] with root cause
java.sql.SQLIntegrityConstraintViolationException: Column 'aaa' cannot be null
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:117)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
数据库字段信息:
`aaa` VARCHAR(255) NOT NULL DEFAULT '' COLLATE 'utf8mb4_general_ci',
public class XXX {
@Column(name = "`aaa`")
private String aaa;
}
请问在数据库提供了默认值的情况下,为什么还会报 Column cannot be null
呢?
JPA默认是全量操作字段,所以这里实际生成的SQL语句还是会向aaa字段插入nul值,如果你想空值就不插入使用数据库默认值,给字段打上@DynamicInsert注解