java - java.sql.SQLException:字段“register_id”没有默认值
问题描述
我已经使用 spring boot 和 spring data jpa 编写了一个休息服务。将数据插入数据库时出现问题。我被搜查了,但没有弄清楚问题所在。
所以,我在 Mysql 中有一个名为user
.
这是 DDL:
'id' INT NOT NULL AUTO_INCREMENT,
'registerId' INT NULL,
'genderId' INT NULL,
'cityId' INT NULL,
'firstName' VARCHAR(255) NULL,
'lastName' VARCHAR(255) NULL,
'username' VARCHAR(255) NULL,
'age' INT(2) NULL,
'email' VARCHAR(100) NULL,
'isNotificationAllowed' BIT(1) NULL,
'isBlocked' BIT(1) NULL,
'isActive' BIT(1) NULL,
'createdAt' TIMESTAMP NULL,
'updatedAt' TIMESTAMP NULL,
这是实体代表java项目中的那些列。
@Data
@Builder
@Entity
@Table(name = "user")
public class UserEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", nullable = false)
private int id;
@Column(name = "registerId", nullable = true)
private int registerId;
@Column(name = "genderId", nullable = true)
private Integer genderId;
@Column(name = "cityId", nullable = true)
private Integer cityId;
@Column(name = "firstName", nullable = true, length = 255)
private String firstName;
@Column(name = "lastName", nullable = true, length = 255)
private String lastName;
@Column(name = "username", nullable = true, length = 255)
private String username;
@Column(name = "age", nullable = true)
private Integer age;
@Column(name = "email", nullable = true, length = 100)
private String email;
@Column(name = "isNotificationAllowed", nullable = true)
private boolean isNotificationAllowed;
@Column(name = "isBlocked", nullable = true)
private boolean isBlocked;
@Column(name = "isActive", nullable = true)
private boolean isActive;
@Column(name = "createdAt", nullable = true)
private Timestamp createdAt;
@Column(name = "updatedAt", nullable = true)
private Timestamp updatedAt;
}
所以,当我通过 Postman 运行服务时,它会在控制台上显示这样的 sql
insert into user (age, city_id, created_at, email, first_name, gender_id, is_active, is_blocked, is_notification_allowed, last_name, register_id, updated_at, username, id)
。
当您发现实体名称与数据库中的列名称不同时。例如:cityId
在我的 POJO 中转换city_id
为 sql。所以,我搜索了它,发现@Column 注释中有一个错误。它没有像我们预期的那样工作。
请看:这是一个链接。
所以,为了能够解决这个问题,我添加了
spring.jpa.hibernate.naming.implicit-
strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
然后重新启动应用程序,似乎列映射已解决:
Hibernate:
insert
into
user
(age, cityId, createdAt, email, firstName, genderId, isActive, isBlocked, isNotificationAllowed, lastName, registerId, updatedAt, username, id)
values
(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
但是在日志的末尾,会抛出一个错误。它说:
java.sql.SQLException: Field 'register_id' doesn't have a default value
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129) ~[mysql-connector-java-8.0.17.jar:8.0.17]
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97) ~[mysql-connector-java-8.0.17.jar:8.0.17]
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) ~[mysql-connector-java-8.0.17.jar:8.0.17]
我不明白这些领域的register_id
出口在哪里。它不存在于数据库或实体中。
当我不在请求registerId
中作为参数发送时,它会显示java.sql.SQLIntegrityConstraintViolationException: Column 'registerId' cannot be null
. 也没有弄清楚那个消息。因为它在数据库中可以为空,并且在实体中定义为可以为空。
解决方案
问题是该registerId
属性被定义为int
,而不是Integer
。适当的列可以null
在数据库中,但该null
值不能转换为int
. 将其更改为Integer
.
推荐阅读
- list - 如何用颤动中的小部件列表替换图像列表?
- node.js - dotenv | 在配置文件中调用 process.env.NAME 时不起作用
- r - 在 Ubuntu-16.04 上安装特定的 R 版本
- typescript - RXJS v6.4 switchMap 操作符返回一个 Observable 而不是结果(根据 TypeScript linter)
- ios - 如何在 iOS 上录制其他应用程序的屏幕内容?是否存在有关隐私(应用沙盒)的安全问题?
- excel - Excel 函数无法区分某些阿拉伯字母
- node.js - 加密时节点openpgp错误:在密钥中找不到用于加密的有效密钥包
- selenium - 如何使用 Selenium 插件执行 Nutch?
- python-3.x - 我的列表的输入发生了变化,我该如何更正?
- sql - Varchar 'YYMMDD' 到日期 'DD.MM.YYYY'