java.sql.SQLException:字段'register_id'没有默认值

时间:2019-10-30 19:51:16

标签: java hibernate spring-boot spring-data-jpa

我正在使用spring boot和spring data jpa编写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(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,

这是Entity,表示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;
}

因此,当我通过邮递员运行服务时,它在控制台上显示了这样的一条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批注中存在错误。它没有按我们预期的那样工作。

请查看:这里是a link

因此,为了解决这个问题,我添加了

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。也没有弄清楚那个消息。因为它在数据库中可以为空,而在实体中定义为可以为空。

1 个答案:

答案 0 :(得分:1)

问题在于registerId属性定义为int,而不是Integer。适当的列可以是数据库中的null,但是该null的值不能转换为int。将其更改为Integer