Skip to content
Snippets Groups Projects
Commit 32dabc3c authored by Dominika Zemanovičová's avatar Dominika Zemanovičová
Browse files

Fix eager loading

parent cbb2879c
No related branches found
No related tags found
1 merge request!32M2 fix exercise
......@@ -29,7 +29,7 @@ public class Exercise extends DomainObject {
private int difficulty;
@Column(name = "lecture_id")
@Column(name = "course_id")
private long courseId;
@OneToMany(mappedBy="exercise", cascade = CascadeType.ALL)
......
......@@ -2,6 +2,7 @@ package org.fuseri.moduleexercise.question;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
......@@ -35,7 +36,7 @@ public class Question extends DomainObject {
@OneToMany(mappedBy = "question", cascade = CascadeType.ALL, orphanRemoval = true)
private Set<Answer> answers = new HashSet<>();
@ManyToOne
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "exercise_id", nullable = false)
private Exercise exercise;
......@@ -46,5 +47,8 @@ public class Question extends DomainObject {
*/
public void addAnswers(Set<Answer> answersToAdd) {
answers.addAll(answersToAdd);
for (var answer : answersToAdd) {
answer.setQuestion(this);
}
}
}
......@@ -95,7 +95,7 @@ public class QuestionController {
@ApiResponse(responseCode = "404", description = "Exercise with the specified ID was not found.")
})
@PostMapping
public ResponseEntity<QuestionDto> addQuestion(@Valid @RequestBody QuestionCreateDto dto) {
public ResponseEntity<QuestionDto> createQuestion(@Valid @RequestBody QuestionCreateDto dto) {
QuestionDto createdQuestionDto = questionFacade.create(dto);
return ResponseEntity.status(HttpStatus.CREATED).body(createdQuestionDto);
}
......
......@@ -83,6 +83,19 @@ public class QuestionService extends DomainService<Question> {
throw new EntityNotFoundException(
"Question with id: " + id + " was not found.");
}
}
/**
* Create a question with answers
*
* @param question the question to create
* @return the created question
*/
@Override
public Question create(Question question) {
for (var answer: question.getAnswers()) {
answer.setQuestion(question);
}
return getRepository().save(question);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment