-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: OneToOne Bidirectional not setting Foreign Key
PostPosted: Tue May 16, 2017 7:32 pm 
Beginner
Beginner

Joined: Sun May 07, 2017 11:24 pm
Posts: 29
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


Top
 Profile  
 
 Post subject: Re: OneToOne Bidirectional not setting Foreign Key
PostPosted: Wed May 17, 2017 3:56 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
You are saving an Episode entity which cascades the persist operation to its associated Plan child entity.

If you place a breakpoint at this line:

Code:
Episode savedEpisode = episodeRepository.save(episode);


You will probably see that the episode reference is null in the associated Plan child entity.

If that's the case, then you need to set it manually since Jackson or your MVC property mapper cannot handle circular references.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.