繁体   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