java.sql.SQLException: 字段 ‘participant_one_fk_id’ 没有默认值

huangapple go评论73阅读模式
英文:

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

问题

以下是翻译好的内容:

我在创建一个包含两名成员的比赛时遇到了问题。我无法将参与者保存到比赛中。
也许问题在于我在项目中使用了两个一对一的注释?

比赛实体类:

@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 主键。

英文:

I am having a problem creating a match with two members. I cannot save participants to the match.
Perhaps the problem is that
do i use two one-to-one anatations in my project?

 @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() {
    }

class participant match

@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

i am trying to save to the repository

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

this is my JSON request

 {
 	"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"
  
 }
 }

writes an error

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

AUTO_INCREMENT PRIMARY KEY available in all classes

答案1

得分: 2

好的,以下是您要求的翻译内容:

似乎您的列participant_one_fk_id没有默认值。
尝试像这样做:

  1. 为列participant_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;

如果出现关于整数的问题,我知道"width"或其他内容已被弃用,所以可能不需要(10),但首先尝试一下带有它。

英文:

It seems that your column participant_one_fk_id does not have a default value.
Try something like this

  1. Add a default value to the column participant_one_fk_id Using -

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

( I think the only first step will do the trick, try right after first step )

  1. Supply some value to the participant_one_fk_id' column during insertion.

  2. Add an auto increment to the column and add a primary key to it using the code :-

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

If it complains about something regarding integer I know that "width" or whatever is deprecated so (10) might won't be required, but try with it first.

huangapple
  • 本文由 发表于 2020年8月28日 20:05:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/63633462.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定