Reputation: 66
I have started to use Hibernate with Panache in my projects.
Basic I have two entities QuestionGroup and Question.
@Entity
@Table(name = "tb006_question_groups")
public class QuestionGroup {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "question_group_id")
@Getter @Setter
private Long id;
@Column(name = "question_group_code")
@Getter @Setter
private String code;
@Column(name = "question_group_name")
@Getter @Setter
private String name;
@CreationTimestamp
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "question_group_createdAt")
@Getter @Setter
private Date createdAt;
@UpdateTimestamp
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "question_group_updatedAt")
@Getter @Setter
private Date updatedAt;
@Column(name = "question_group_enabled")
@Getter @Setter
private Integer enabled;
@OneToMany(mappedBy="questionGroup", cascade = CascadeType.PERSIST)
@Getter @Setter
private List<Question> questions;
@Entity
@Table(name = "tb007_questions")
public class Question {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "question_id")
@Getter @Setter
private Long id;
@Column(name = "question_name")
@Getter @Setter
private String name;
@Column(name = "question_answer")
@Getter @Setter
private String answer;
@CreationTimestamp
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "question_createdAt")
@Getter @Setter
private Date createdAt;
@UpdateTimestamp
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "question_updatedAt")
@Getter @Setter
private Date updatedAt;
@Column(name = "question_enabled")
@Getter @Setter
private Integer enabled;
@ManyToOne
@JoinColumn(name="question_group_id", nullable=false)
@Getter @Setter
private QuestionGroup questionGroup;
This below, the method to insert the datas
@Transactional
@Override
public QuestionGroup createQuestionGroup(QuestionGroupCreateRequestDTO questionGroupCreate) {
QuestionGroup questionGroup =
this.convertQuestionGroupCreateToQuestionGroup(questionGroupCreate);
if (questionGroupCreate.getQuestions() != null) {
List<Question> questions = questionGroupCreate.getQuestions().stream().map(question -> this.convertQuestionCreateToQuestion(question)).collect(Collectors.toList());
questionGroup.setQuestions(questions);
}
questionGroupRepository.persist(questionGroup);
return questionGroup;
}
In my case, the entity QuestionGroup is persist correctly, after that my questions are not persisting and I am receiving this message: Column 'question_group_id' cannot be null
I am imaging the id from QuestionGroup not propagating to Question
How to resolve this problem without persist QuestionGroup and after create the Question objects?
Upvotes: 0
Views: 815
Reputation: 129
Please initialize your list inside QuestionGroup:
@OneToMany(mappedBy="questionGroup", cascade = CascadeType.PERSIST)
@Getter @Setter
private List<Question> questions = new ArrayList<>();
Inside your QuestionGroup class put a helper method like this:
public void addQuestion(Question question) {
questions.add(question);
question.setQuestionGroup(this);
}
And change this line in your createQuestionGroup:
List<Question> questions = questionGroupCreate.getQuestions().stream().map(question -> this.convertQuestionCreateToQuestion(question)).collect(Collectors.toList());
to:
List<Question> questions = questionGroupCreate.getQuestions().stream().map(question -> {
var questionConverted = this.convertQuestionCreateToQuestion(question);
questionGroup.addQuestion(questionConverted);
}).collect(Collectors.toList());
Upvotes: 1