Hello,
I am having trouble with a foreign key episode_id on plan table being null during submit. Some background:
I am using java, springboot (hibernate 5), HTML, thyme leaf and IntelliJ is my IDE. I am very new to hibernate.
I have a BiDir OneToOne relationship between Episode and Plan.
Code:
@Entity
public class Episode {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToOne(mappedBy = "episode", cascade = CascadeType.ALL)
private Plan plan;
@Entity
public class Plan {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToOne
@JoinColumn(name = "episode_id", referencedColumnName = "id", nullable = false)
private Episode episode;
The Episode event is created and saved. It isn't until a bit later that the details of the plan related to the episode are entered. When I go to click the submit button on the plan is when I get the error:
Code:
There was an unexpected error (type=Internal Server Error, status=500).
could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
The post and the get methods for the plan page is:
Code:
@GetMapping("/plan")
public String Episode(Model model, Episode episode) {
List<User> users = userService.findAll();
model.addAttribute("users", users);
model.addAttribute("episode", episode);
return "plan";
}
@PostMapping("/planSubmit")
public String submitPlan(@ModelAttribute Episode episode) {
episodeService.saveEpisode(episode);
return "episode";
}
The actual error is triggered on the EpisodeServiceImpl:
Code:
Episode savedEpisode = episodeRepository.save(episode);
return episodeRepository.save(episode);
And the console output:
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'episode_id' cannot be null
What am I doing wrong? why id of episode not going into episode_id on plan?
Thanks