[英]java.sql.SQLException: Field 'register_id' doesn't have a default value
I've writing a rest service using spring boot and spring data jpa.我已经使用 spring 引导和 spring 数据 ZF0B4A29AE293615D6 I got problem when inserting a data to database.
将数据插入数据库时出现问题。 I got searched but not figure out the problem.
我被搜查了,但没有弄清楚问题所在。
So, I have a table in Mysql called user
.所以,我在 Mysql 中有一个名为
user
的表。
This is the DDL:这是 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,
And this is the Entity represent those columns in java project.这是代表 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;
}
So, when I run the service via Postman, it shows an sql on console like this 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)
. So, when I run the service via Postman, it shows an sql on console like this
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)
.
As you figure out entity names are different than the column names in database.当您发现实体名称与数据库中的列名称不同时。 For ex:
cityId
in my POJO transformed city_id
in sql.例如:我的 POJO 中的
cityId
转换了city_id
中的 city_id。 So, I've search it and found that there is a bug in @Column annotation.所以,我搜索了它,发现@Column 注释中有一个错误。 It is not working as we expected.
它没有像我们预期的那样工作。
Please look at: Here's a link .请看:这是一个链接。
So,to be able to solve this problem I've added所以,为了能够解决这个问题,我添加了
spring.jpa.hibernate.naming.implicit-
strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
Then restarted the application and the it seems column mapping has resolved:然后重新启动应用程序,似乎列映射已解决:
Hibernate:
insert
into
user
(age, cityId, createdAt, email, firstName, genderId, isActive, isBlocked, isNotificationAllowed, lastName, registerId, updatedAt, username, id)
values
(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
But at the end of the logs, there is an error thrown.但是在日志的末尾,会抛出一个错误。 It says:
它说:
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]
I don't understand where the fieds register_id
exits.我不明白 fieds
register_id
在哪里退出。 It doesn't exist in database or entity.它不存在于数据库或实体中。
When I don't send registerId
as parameter in the request, it says java.sql.SQLIntegrityConstraintViolationException: Column 'registerId' cannot be null
.当我没有在请求中发送
registerId
作为参数时,它java.sql.SQLIntegrityConstraintViolationException: Column 'registerId' cannot be null
. Also didn't figure out that message.也没有弄清楚那个消息。 Because it is nullable in database and defined nullable in entity.
因为它在数据库中可以为空,并且在实体中定义为可以为空。
The problem is that the registerId
property is defined as int
, not as Integer
.问题是
registerId
属性被定义为int
,而不是Integer
。 the appropriate column can be null
in the database, but that null
value cannot be converted to int
.相应的列可以是数据库中的
null
,但不能将null
值转换为int
。 Change it to Integer
.将其更改为
Integer
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.