java.sql.SQLException:字段';注册号';不';没有默认值
我已经使用SpringBoot和SpringDataJPA编写了一个rest服务。 我在将数据插入数据库时遇到问题。我被搜身了,但没找到问题所在 因此,我在Mysql中有一个名为java.sql.SQLException:字段';注册号';不';没有默认值,java,hibernate,spring-boot,spring-data-jpa,Java,Hibernate,Spring Boot,Spring Data Jpa,我已经使用SpringBoot和SpringDataJPA编写了一个rest服务。 我在将数据插入数据库时遇到问题。我被搜身了,但没找到问题所在 因此,我在Mysql中有一个名为user的表 这是DDL: 'id' INT NOT NULL AUTO_INCREMENT, 'registerId' INT NULL, 'genderId' INT NULL, 'cityId' INT NULL, 'firstName' VARCHAR(255) NULL, 'lastName' VARCHAR(
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,如下所示
插入用户(年龄、城市id、创建地址、电子邮件、姓名、性别id、是否活动、是否阻止、是否允许通知、姓氏、注册id、更新地址、用户名、id)
当您发现实体名称不同于数据库中的列名时。例如:cityId
在我的POJO中转换为city\u id
在sql中。所以,我搜索了一下,发现@Column注释中有一个bug。它没有像我们预期的那样工作
请看:这是我的
所以,为了能够解决这个问题,我添加了
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]
我不明白fiedsregister\u id
的出口在哪里。它不存在于数据库或实体中
当我不将
registerId
作为请求中的参数发送时,它会显示java.sql.SQLIntegrityConstraintViolationException:Column'registerId'不能为null
。我也不明白那条信息。因为它在数据库中可以为null,在实体中定义为null。问题在于registerId
属性被定义为int
,而不是Integer
。数据库中适当的列可以是null
,但该null
值不能转换为int
。将其更改为Integer
如果Java实体上的字段名与列名匹配,则无需将name=registerId
部分添加到@column
注释中,只需执行@column(nullable=true)即可
spring.jpa.hibernate.naming.implicit strategy=org.hibernate.boot.model.naming.implicit namingstrategylegacyjpaimpl spring.jpa.hibernate.naming.PhysicalNamingStrategyStandardImpl
你把这些属性放进去了吗?@JonathanJohx是的,我已经在问题中解释过了。它有效吗?你有什么错误?你添加这个问题的那些错误?@JonathanJohx我已经在问题中一步一步地解释了。是的,错误仍然存在。我已更改了它的数据类型,但仍然抛出相同的错误。在问题说明的末尾用新的案例更新了问题。