field' playerId'没有默认值

发布于 2025-01-23 06:43:43 字数 2610 浏览 0 评论 0原文

我有两个类,playerplayergoalsprediction

请求从前端发送,其中包含playerId目标值。 playerId用于使用player使用player> PlayerGoalsPrediction,以及目标

但是,当将对象保存到db时,返回错误(java.sql.sqleclection:field'playerId'没有默认值)。

我认为它一定来自playergoalsPrediction.player属性,但是该对象确实具有分配给它的playerId值,所以我不确定问题来自何处?

player.java

@Entity
@Table(name = "Players")
public class Player {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "PlayerID")
    private long playerID;
    
    @Column(name = "Name")
    private String name;

    @OneToMany(
        targetEntity=PlayerGoalsPrediction.class,
        fetch=FetchType.LAZY,
        mappedBy="player"
    )
    private List<PlayerGoalsPrediction> predictions;

    // constructors, getters and setters
}

playergoalsPrediction.java

@Entity
@Table(name = "PlayerGoalsPredictions")
public class PlayerGoalsPrediction {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "PlayerGoalsPredictionID")
    private long playerGoalsPredictionID;

    @ManyToOne(
        targetEntity=Player.class,
        fetch=FetchType.EAGER
    )
    @Column(name = "PlayerID")
    private Player player;

    @Column(name = "Goals")
    private long goals;

    // constructors, getters and setters
}

playerRepository.java

@Repository
public interface PlayerRepository extends JpaRepository<Player, String>{
    
    public Player findByPlayerID(long playerID);
}

playergoalsgoalsPredictionController.java

@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/api/v1/playergoalsprediction")
public class PlayerGoalsPredictionController {

    @Autowired
    private PlayerRepository playerRepository;

    @Autowired
    private PlayerGoalsPredictionRepository playerGoalsPredictionRepository;

    @PostMapping("/postprediction")
    public void postPredictions(
            @Valid @RequestBody PlayerGoalsPredictionRequest playerGoalsPredictionRequest 
    ) {
        PlayerGoalsPrediction playerGoalsPrediction = new PlayerGoalsPrediction(
            playerRepository.findByPlayerID(playerGoalsPredictionRequest.getPlayerID()),
            playerGoalsPredictionRequest.getGoals()
        );
        playerGoalsPredictionRepository.save(playerGoalsPrediction);
    }
}

I have two classes, Player and PlayerGoalsPrediction.

Requests are sent in, from the frontend, containing a playerID and goals value. The playerID is used to find the Player using the playerRepository and then the Player object is used in the constructor of a PlayerGoalsPrediction, along with the goals.

However, when save the object to the db, an error (java.sql.SQLException: Field 'PlayerID' doesn't have a default value) is returned.

I assume it must be coming from the PlayerGoalsPrediction.player attribute, but that object does have a playerID value assigned to it, so I am unsure where the issue is coming from?

Player.java

@Entity
@Table(name = "Players")
public class Player {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "PlayerID")
    private long playerID;
    
    @Column(name = "Name")
    private String name;

    @OneToMany(
        targetEntity=PlayerGoalsPrediction.class,
        fetch=FetchType.LAZY,
        mappedBy="player"
    )
    private List<PlayerGoalsPrediction> predictions;

    // constructors, getters and setters
}

PlayerGoalsPrediction.java

@Entity
@Table(name = "PlayerGoalsPredictions")
public class PlayerGoalsPrediction {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "PlayerGoalsPredictionID")
    private long playerGoalsPredictionID;

    @ManyToOne(
        targetEntity=Player.class,
        fetch=FetchType.EAGER
    )
    @Column(name = "PlayerID")
    private Player player;

    @Column(name = "Goals")
    private long goals;

    // constructors, getters and setters
}

PlayerRepository.java

@Repository
public interface PlayerRepository extends JpaRepository<Player, String>{
    
    public Player findByPlayerID(long playerID);
}

PlayerGoalsPredictionController.java

@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/api/v1/playergoalsprediction")
public class PlayerGoalsPredictionController {

    @Autowired
    private PlayerRepository playerRepository;

    @Autowired
    private PlayerGoalsPredictionRepository playerGoalsPredictionRepository;

    @PostMapping("/postprediction")
    public void postPredictions(
            @Valid @RequestBody PlayerGoalsPredictionRequest playerGoalsPredictionRequest 
    ) {
        PlayerGoalsPrediction playerGoalsPrediction = new PlayerGoalsPrediction(
            playerRepository.findByPlayerID(playerGoalsPredictionRequest.getPlayerID()),
            playerGoalsPredictionRequest.getGoals()
        );
        playerGoalsPredictionRepository.save(playerGoalsPrediction);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文