JPA 数据库已经设置默认值了. 为什么保存时报错 Column cannot be null ?

错误信息:

 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 呢?

阅读 7.6k
1 个回答

JPA默认是全量操作字段,所以这里实际生成的SQL语句还是会向aaa字段插入nul值,如果你想空值就不插入使用数据库默认值,给字段打上@DynamicInsert注解

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题