field' playerId'没有默认值
我有两个类,player
和playergoalsprediction
。
请求从前端发送,其中包含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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论