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

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

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

问题

以下是翻译好的内容:

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

比赛实体类:

  1. @Entity
  2. @Table(name = "mach")
  3. public class TournamentMatch {
  4. @Id
  5. @GeneratedValue(strategy = GenerationType.IDENTITY)
  6. @Column(name = "mach_id")
  7. private int id;
  8. private LocalDate startTime;
  9. private LocalDate finischTime;
  10. private BigDecimal scores;
  11. @ManyToOne
  12. @JoinColumn(name = "tournament_fk_id")
  13. private Tournament tournament;
  14. @OneToOne(optional = false, cascade = {CascadeType.PERSIST, CascadeType.REMOVE, CascadeType.MERGE})
  15. @JoinColumn(name = "participant_two_fk_id")
  16. private Participant participantTwo;
  17. @OneToOne(optional = false, cascade = {CascadeType.PERSIST, CascadeType.REMOVE, CascadeType.MERGE})
  18. @JoinColumn(name = "participant_one_fk_id")
  19. private Participant participantOne;
  20. public TournamentMatch() {
  21. }
  22. }

选手实体类:

  1. @Entity
  2. public class Participant {
  3. @Id
  4. @GeneratedValue(strategy = GenerationType.IDENTITY)
  5. @Column(name = "participant_id")
  6. private int id;
  7. @NotNull
  8. @Column(name = "nick_name")
  9. private String nickName;
  10. public Participant() {
  11. }
  12. // gets, sets
  13. }

我尝试保存到仓库中:

  1. @Override
  2. public void save(TournamentMatch match) {
  3. tournamentMatchRepository.save(match);
  4. }

这是我的 JSON 请求:

  1. {
  2. "id": 0,
  3. "startTime": "2018-10-10",
  4. "finischTime": "2018-10-11",
  5. "scores": "3",
  6. "tournament": {
  7. "id": 0,
  8. "name": "ChempionsLigue1"
  9. },
  10. "participantOne": {
  11. "id": 0,
  12. "nickName": "PlayerOne"
  13. },
  14. "participantTwo": {
  15. "id": 0,
  16. "nickName": "PlayerTwo"
  17. }
  18. }

出现了错误:

  1. 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?

  1. @Entity
  2. @Table(name = "mach")
  3. public class TournamentMatch {
  4. @Id
  5. @GeneratedValue(strategy = GenerationType.IDENTITY)
  6. @Column(name = "mach_id")
  7. private int id;
  8. private LocalDate startTime;
  9. private LocalDate finischTime;
  10. private BigDecimal scores;
  11. @ManyToOne
  12. @JoinColumn(name = "tournament_fk_id")
  13. private Tournament tournament;
  14. @OneToOne(optional = false, cascade = {CascadeType.PERSIST, CascadeType.REMOVE, CascadeType.MERGE})
  15. @JoinColumn(name = "participant_two_fk_id")
  16. private Participant participantTwo;
  17. @OneToOne(optional = false, cascade = {CascadeType.PERSIST, CascadeType.REMOVE, CascadeType.MERGE})
  18. @JoinColumn(name = "participant_one_fk_id")
  19. private Participant participantOne;
  20. public TournamentMatch() {
  21. }

class participant match

  1. @Entity
  2. public class Participant {
  3. @Id
  4. @GeneratedValue(strategy = GenerationType.IDENTITY)
  5. @Column(name = "participant_id")
  6. private int id;
  7. @NotNull
  8. @Column(name = "nick_name")
  9. private String nickName;
  10. public Participant() {
  11. }
  12. ...gets,sets

i am trying to save to the repository

  1. @Override
  2. public void save(TournamentMatch match) {
  3. tournamentMatchRepository.save(match);
  4. }

this is my JSON request

  1. {
  2. "id": 0,
  3. "startTime": "2018-10-10",
  4. "finischTime": "2018-10-11",
  5. "scores": "3",
  6. "tournament":{
  7. "id": 0,
  8. "name": "ChempionsLigue1"
  9. },
  10. "participantOne":{
  11. "id": 0,
  12. "nickName": "PlayerOne"
  13. },
  14. "participantTwo":{
  15. "id": 0,
  16. "nickName": "PlayerTwo"
  17. }
  18. }

writes an error

  1. 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:

确定