java在springmvc中Map外键

q3aa0525  于 2021-07-26  发布在  Java
关注(0)|答案(0)|浏览(179)

我是一个新的自学程序员,我做我的第一个独立项目,并遇到一些问题。我正在制作一个测验应用程序,我想为测验创建一个类,并为Map到每个测验的问题创建另一个类。我创建了我认为应该是适当的模型、视图和控制器,但是在mysql中,没有将问题和测验Map在一起。我做了一个类似的ish教程,似乎hibernate连接了数据,但这次没有。
我曾尝试在quizcreatecontroller中重写processcreatenewquick方法,但至今没有成功。关于我应该研究什么来取得进展有什么想法吗?
以下是我认为的相关代码:
测验课

package org.launchcode.trivia.models;

import javax.persistence.Entity;
import javax.persistence.OneToMany;
import java.util.ArrayList;
import java.util.List;

@Entity
public class Quiz extends AbstractEntity {

    public String name;

    @OneToMany (mappedBy = "quiz")
    private List<QuestionAnswerInfo> questions = new ArrayList<>();

    public Quiz(){}

    public Quiz(String name, ArrayList<QuestionAnswerInfo> questions) {
        super();
        this.name = name;
        this.questions = questions;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<QuestionAnswerInfo> getQuestions() {
        return questions;
    }

    public void setQuestions(List<QuestionAnswerInfo> questions) {
        this.questions = questions;
    }

    public void addQuestions(List<QuestionAnswerInfo> questions, QuestionAnswerInfo question) {
        questions.add(question);
    }
}

questionanswerinfo类

package org.launchcode.trivia.models;

import javax.persistence.Entity;
import javax.persistence.ManyToOne;

@Entity
public class QuestionAnswerInfo extends AbstractEntity{

    private String question;
    private String answer;
    private String questionType;
    private String additionalAnswerInfo;

    @ManyToOne
    private Quiz quiz;

    public QuestionAnswerInfo (){}

    public QuestionAnswerInfo(String question, String answer, String questionType, String additionalAnswerInfo,
                              Quiz quiz) {
        super();
        this.question = question;
        this.answer = answer;
        this.questionType = questionType;
        this.additionalAnswerInfo = additionalAnswerInfo;
        this.quiz = quiz;
    }

    public String getQuestion() {
        return question;
    }

    public void setQuestion(String question) {
        this.question = question;
    }

    public String getAnswer() {
        return answer;
    }

    public void setAnswer(String answer) {
        this.answer = answer;
    }

    public String getQuestionType() {
        return questionType;
    }

    public void setQuestionType(String questionType) {
        this.questionType = questionType;
    }

    public String getAdditionalAnswerInfo() {
        return additionalAnswerInfo;
    }

    public void setAdditionalAnswerInfo(String additionalAnswerInfo) {
        this.additionalAnswerInfo = additionalAnswerInfo;
    }
}

QuizCreate控制器

package org.launchcode.trivia.controllers;

import org.launchcode.trivia.models.QuestionAnswerInfo;
import org.launchcode.trivia.models.Quiz;
import org.launchcode.trivia.models.data.QuestionRepository;
import org.launchcode.trivia.models.data.QuizRepository;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

@Controller
public class QuizCreateController {

    @Autowired
    private QuestionRepository questionRepository;

    @Autowired
    private QuizRepository quizRepository;

    @RequestMapping("create")
    public String displayCreateNewQuiz(Model model) {
        model.addAttribute(new Quiz());
        model.addAttribute("questions", new QuestionAnswerInfo());
        return "create";
    }

    @PostMapping("create")
    public String processCreateNewQuiz(@ModelAttribute Quiz newQuiz, @ModelAttribute QuestionAnswerInfo questions,
                                       Model model) {
        List<QuestionAnswerInfo> quizQuestions = new ArrayList<>();
        quizQuestions.add(questions);
        newQuiz.setQuestions(quizQuestions);
        quizRepository.save(newQuiz);
        questionRepository.save(questions);

        return "index";
    }
}

创建.html视图

<!DOCTYPE html>
<html lang="en" xmlns:th="https://www.thymeleaf.org/">
<head>
    <meta charset="UTF-8"/>
    <title>Title</title>
</head>
<body>
<h1>Create a Quiz</h1>

<form method="post">
    <div>
        <label th:for="name">Quiz Name</label>
        <input th:field="${quiz.name}"/>
    </div>
    <br>
    <div>
        <label th:for="question">Add a Question</label>
        <input th:field="${questions.question}"/>
    </div>
    <div>
        <label th:for="answer">Add an Answer</label>
        <input th:field="${questions.answer}"/>
    </div>
    <div>
        <label th:for="questionType">Question Type</label>
        <input th:field="${questions.questionType}"/>
    </div>
    <div>
        <label th:for="additionalAnswerInfo">Add Additional Information</label>
        <input th:field="${questions.additionalAnswerInfo}"/>
    </div>

    <input type="submit" value="Create Quiz"/>
</form>

</body>
</html>

如果你需要更多的信息来帮助我,请告诉我。我还在学习如何寻求帮助!
谢谢!

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题