@FreddyMF

Почему Hibenate не создает сущность?

Есть таблицы Questions и Answers, связанные через @OneToMany и @ManyToOne. При добавлении ответа к вопросу ничего не происходит.
Ответственный контроллер
@Controller
@RequestMapping("/answers")
public class AnswersController {

    private final String BASEDIR = "/answers";

    private final AnswersDAO answersDAO;
    private final QuestionsDAO questionsDAO;

    public AnswersController(AnswersDAO answersDAO, QuestionsDAO questionsDAO) {
        this.answersDAO = answersDAO;
        this.questionsDAO = questionsDAO;
    }

    @GetMapping("")
    public String main(){
        return "redirect:".concat("/student");
    }
    @GetMapping("/new")
    public String createAnswer(Model model) {
        model.addAttribute("answer", new Answers());
        model.addAttribute("questions", questionsDAO.getQuestionsList());
        return BASEDIR.concat("/answers_create");
    }

    @PostMapping
    public String create(@ModelAttribute(name = "answer") @Valid Answers answer,
                         BindingResult bindingResult) {
        if (bindingResult.hasErrors())
            return "redirect:".concat(BASEDIR.concat("/new"));
        answersDAO.create(answer);
        System.out.println(1111);
        return "redirect:".concat(BASEDIR);
    }
}

Questions.java
@Entity
public class Questions {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @NotEmpty
    private String question;

    @NotEmpty
    private String author;

    @NotEmpty
    @Column(name = "correct_answer")
    private String correctAnswer;

    @OneToMany(mappedBy = "answer", cascade = CascadeType.ALL)
    private List<Answers> answers;

    public Questions() {
    }
}

Answers.java
@Entity
@Table(name = "answers")
public class Answers {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private int id;

    @NotEmpty
    @Column(name = "answer")
    private String answer;

    @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name = "question_id", foreignKey = @ForeignKey(name = "FK_QUESTION_ID"))
    private Questions question;

    public Answers() {
    }
}

Представление
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form th:method="POST" th:action="@{/answers}" th:object="${answer}">
    <label for="answer">Введите ответ</label>
    <input type="text" th:field="*{answer}" id="answer">
    <br>
    <label for="questionsList">К какому вопросу?</label>
    <select id="questionsList" name="question">
        <option th:each="questionq : ${questions}"
                th:text="${questionq.getQuestion()}"
                th:value="${questionq}">
            value
        </option>
    </select>
    <br>
    <input type="submit" value="Добавить">
</form>
</body>
</html>
  • Вопрос задан
  • 67 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы
Bell Integrator Ульяновск
До 400 000 ₽
Bell Integrator Ижевск
До 400 000 ₽
Bell Integrator Хабаровск
До 400 000 ₽
29 мар. 2024, в 16:38
5000 руб./за проект
29 мар. 2024, в 16:25
7000 руб./за проект
29 мар. 2024, в 16:22
30000 руб./за проект