簡體   English   中英

java.sql.SQLException:字段“register_id”沒有默認值

[英]java.sql.SQLException: Field 'register_id' doesn't have a default value

我已經使用 spring 引導和 spring 數據 ZF0B4A29AE293615D6 將數據插入數據庫時出現問題。 我被搜查了,但沒有弄清楚問題所在。

所以,我在 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;
}

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) .

當您發現實體名稱與數據庫中的列名稱不同時。 例如:我的 POJO 中的cityId轉換了city_id中的 city_id。 所以,我搜索了它,發現@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]

我不明白 fieds register_id在哪里退出。 它不存在於數據庫或實體中。

當我沒有在請求中發送registerId作為參數時,它java.sql.SQLIntegrityConstraintViolationException: Column 'registerId' cannot be null . 也沒有弄清楚那個消息。 因為它在數據庫中可以為空,並且在實體中定義為可以為空。

問題是registerId屬性被定義為int ,而不是Integer 相應的列可以是數據庫中的null ,但不能將null值轉換為int 將其更改為Integer

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM