簡體   English   中英

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

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

我在創建與兩個成員的匹配時遇到問題。 我無法將參與者保存到比賽中。 也許問題是我在我的項目中使用了兩個一對一的 anatations 嗎?

 @Entity
    @Table(name = "mach")
    public class TournamentMatch {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "mach_id")
    private int id;

    private LocalDate startTime;

    private LocalDate finischTime;

    private BigDecimal scores;

    @ManyToOne
    @JoinColumn(name = "tournament_fk_id")
    private Tournament tournament;

    @OneToOne(optional = false, cascade = {CascadeType.PERSIST, CascadeType.REMOVE, CascadeType.MERGE})
    @JoinColumn(name = "participant_two_fk_id")
    private Participant participantTwo;

    @OneToOne(optional = false, cascade = {CascadeType.PERSIST, CascadeType.REMOVE, CascadeType.MERGE})
    @JoinColumn(name = "participant_one_fk_id")
    private Participant participantOne;

    public TournamentMatch() {
    }

班級參加比賽

@Entity
public class Participant {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "participant_id")
    private int id;

    @NotNull
    @Column(name = "nick_name")
    private String nickName;

    public Participant() {
    }
...gets,sets

我正在嘗試保存到存儲庫

  @Override
    public void save(TournamentMatch match) {
        tournamentMatchRepository.save(match);
    }

這是我的 JSON 請求

 {
    "id": 0,
    "startTime": "2018-10-10",
    "finischTime": "2018-10-11",
    "scores": "3",
  
    
   "tournament":{
   "id": 0,
   "name": "ChempionsLigue1"
 },
      "participantOne":{
   "id": 0,
   "nickName": "PlayerOne"
  
 },
  "participantTwo":{
   "id": 0,
   "nickName": "PlayerTwo"
  
 }
 }

寫一個錯誤

java.sql.SQLException: Field 'participant_one_fk_id' doesn't have a default value

AUTO_INCREMENT PRIMARY KEY 適用於所有類

似乎您的列參與者_one_fk_id 沒有默認值。 嘗試這樣的事情

  1. 將默認值添加到列參與者_one_fk_id 使用 -

    ALTER TABLE 'xxx' ALTER 'participant_one_fk_id' SET DEFAULT NULL

(我認為唯一的第一步可以解決問題,請在第一步之后嘗試)

  1. 在插入期間為participant_one_fk_id'列提供一些值。

  2. 向列添加自動增量並使用以下代碼為其添加主鍵:-

    ALTER TABLE 'xxx' CHANGE 'participant_one_fk_id' 'participant_one_fk_id' INT(10)AUTO_INCREMENT PRIMARY KEY;

如果它抱怨有關整數的某些內容,我知道“寬度”或任何已棄用的內容,因此可能不需要 (10),但請先嘗試使用它。

暫無
暫無

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

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