Deepu Nair
Deepu Nair

Reputation: 165

SpringBoot_JPA: OneToMany (bidirectional) not inserting foreign key in child table

SpringBoot Version:1.4.2.RELEASE MySQL Version:5.7.16 Lombok

I have two entity classes Question and Options where question_id from Question table is foreign key in Question_Option table.

Question Entity

@Entity
@Table(name = "questions")
public class Question {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Getter
@Setter
private int questionId;
@Getter
@Setter
@OneToMany(mappedBy = "question",fetch=FetchType.LAZY,   cascade=CascadeType.ALL)
private List<Option> option;

Options Entity

@Entity
@Table(name = "questions_options")
public class Option {
@Id
@GeneratedValue
@Getter
@Setter
private int id;
@Getter
@Setter
private String optionsId;
@Getter
@Setter
 private String optionText;
@ManyToOne
@JoinColumn(name="questionId")
@Getter
@Setter
private Question question;
}

Problem Statement When I try to POST, foreign key (question_id) is not inserted in options table.

Hibernate: insert into questions (questionId, LastUpdate, active, createTime, questionText, questionType) values (default, ?, ?, ?, ?, ?)
Hibernate: insert into questions_options (id, lastUpdate, optionText, optionsId, questionId) values (default, ?, ?, ?, ?)
Hibernate: insert into questions_options (id, lastUpdate, optionText, optionsId, questionId) values (default, ?, ?, ?, ?)

Hibernate: insert into questions_options (id, lastUpdate, optionText, optionsId, questionId) values (default, ?, ?, ?, ?)

How did I fix this ? I changed Question entity to

@Getter
@Setter
@OneToMany(fetch=FetchType.LAZY, cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE})
@JoinColumn(name="questionId")
@NotNull
private List<Option> option;

and Options to

@ManyToOne
@Getter
@Setter
@JoinColumn(name="questionId")
private Question question;

But I want to know why first approach is not working and what am I doing wrong.

Upvotes: 2

Views: 2661

Answers (2)

Udith Indrakantha
Udith Indrakantha

Reputation: 970

This is how it should be in the controller class.

@PostMapping("/question")
public question addQuestion(@RequestBody Question question) {

    if( question.getOption().size() > 0 )
    {
        question.getOption().stream().forEach( option -> {
            option.setQuestion( question );
        } );
    }

   // You can save this question object now using the repository instance autowired.
    return question;
}

One more thing, in the Option class entitiy, you need to add @JsonIgnore annotation as below to avoid recursive fetching

@ManyToOne
@Getter
@Setter
@JoinColumn(name="questionId")
@JsonIgnore
private Question question;

Upvotes: 0

Suman Behara
Suman Behara

Reputation: 160

Try below relationship it should work.

Question Entity

@OneToMany(mappedBy = "question",fetch=FetchType.LAZY,cascade=CascadeType.ALL, targetEntity = Option.class)
private List<Option> option

Options Entity

@ManyToOne
@JoinColumn(name = "questionId")
private Question question;

Upvotes: 1

Related Questions