'I need to map one to many relationship between questions and answer entities. But questionID colum is not updating. How can I fix this?
I used question_id as the primary key of questions table and it is a foreign key for the answers table. @JoinColumn has used for declare referencedColumnName .
@Entity
@Table(name = "questions")
public class Question {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long question_id;
@Column(nullable = false, unique = false, length = 100)
private String question_subjectArea;
@Column(nullable = false, unique = false, length = 1000)
private String fullQuestion;
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn( name = "questionID", referencedColumnName = "question_id")
List<Answer> answer = new ArrayList<>();
//Getters and setters
@Entity
@Table(name = "answers")
public class Answer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long answer_id;
@Column(nullable = true, unique = false, length = 100)
private Long answer_authorID;
@Column(nullable = false, unique = false, length = 100)
private String fullAnswer;
//Getters and setters
Application.properties configuration as follows
spring.jpa.hibernate.ddl-auto=create
Solution 1:[1]
In our Entity class: Answer, seems you need also define the member: Question.
@ManyToOne
@JsonIgnore
@JoinColumn(name = "question", referencedColumnName = "question_id")
private Question question;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | SeanH |