代码之家  ›  专栏  ›  技术社区  ›  StuartDTO

spring jpa创建问题

  •  0
  • StuartDTO  · 技术社区  · 6 年前

    我正在尝试创建一个“游戏”,这只是为了学习,我想知道如何使用JPA创建一个像学习游戏一样的好方法,例如:

    我主要有这些课/桌子

    Question : text, description, set<Answer>, difficulty, userWhoCreated, Topic
    Topic : name, set<question>
    SubTopic : name, set<question>
    Answer : text, question (to reference to it)
    Quiz : set<question>, name, description
    

    但现在我想有一个存储库来存储所有这些问题,所以当用户想学习一点的时候,只需从存储库中获取问题。

    主题和副标题的目标是在用户想要问题时过滤掉,例如。

    例子

    问:什么是加入? 主题是数据库 副标题是连接

    你能指导我怎么做吗?

    我的问题类示例

    @Entity(name = "question")
    public class Question extends DateAudit {
        @Id
        @Column(name = "question_id")
        @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "question_seq")
        @SequenceGenerator(name = "question_seq", allocationSize = 1)
        private Long id;
    
        @Column(name = "name")
        @NotBlank(message = "Question name can not be blank")
        private String name;
    
        @Column(name = "is_exam_question", nullable = false)
        private Boolean is_exam_question;
    
        @ManyToOne(fetch = FetchType.EAGER, cascade = { CascadeType.PERSIST, CascadeType.MERGE })
        private Set<Answer> answers = new HashSet<>();
    
    }
    

    示例应答实体

    @Entity(name = "answer")
    public class Answer extends DateAudit {
    
        @Id
        @Column(name = "answer_id")
        @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "answer_seq")
        @SequenceGenerator(name = "answer_seq", allocationSize = 1)
        private Long id;
    
        @Column(name = "answer_to_question")
        @NotBlank(message = "Answer to question name can not be blank")
        private String answer_to_question;
    
        @ManyToOne
        private Question question;
    
        @Column(name="type_answer")
        private AnswerType answerType;
    }
    

    我也看到,我不能创造答案像“真/假”,“是/否”,“小说明”,“多选择”,我该怎么处理它?

    0 回复  |  直到 6 年前
        1
  •  1
  •   Alan Hay    6 年前

    因此主题可以是一个“自引用”实体,即可以有一个可选的父主题和一个可选的子主题集合。

    主题可以嵌套到任何级别:TopICA和TopICA1和G.

    通过在主题中编写递归函数,我们可以遍历树,并且在层次结构中的任何级别上的主题获得该主题及其所有子主题的问题。

    主题:

    Entity
    @Table(name = "topics")
    public class Topic{
    
        @Id
        private Long id;
    
        @OneToMany(mappedBy = "parent")
        private Set<Topic> subTopics;
    
        @ManyToOne
        @JoinColumn(name = "parent_id")
        private Topic parent;
    
        @OneToMany(mappedBy = "topic")
        private Set<Question> questions;
    
        //questions for this exact topic
        public Set<Question> getQuestions(){
            return questions;
        }
    
        //questions for this topic and all its sub-topics
        public Set<Question> getAllQuestions(){
            return getAllQuestions(this);
        }
    
        //recursive function to walk the topic tree and get all questions for each sub-topic
        private Set<Question> getAllQuestions(Topic topic){
            Set<Question> questions = new HashSet<>(topic.getQuestions());
    
            for(Topic subTopic : topic.getSubTopics()){
                questions.addAll(getAllQuestions(subTopic));
            )
    
            return  questions;
        }
    }
    

    问题:

    @Entity
    @Table(name = "questions")
    public class Question {
    
        @ManyToOne
        @JoinColumn(name = "topic_id")
        private Topic topic;
    }
    

    因此,通过对一个主题的引用,我可以得到它的直接问题或者它的所有问题加上它的所有子主题的问题(以及它们的所有子主题…)

    Topic topic = topicRepository.findOne(someId);
    
    //only questions directly linked to this topic
    Set<Question> questions = topic.getQuestions();
    
    //all questions linked to this topic and its sub-topics to *n* levels.
    Set<Question> questions = topic.getAllQuestions();