Spring JPA创建问题 [英] Spring JPA Create questions

查看:131
本文介绍了Spring JPA创建问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个游戏",这只是为了学习,我想知道如何像研究游戏那样用JPA进行创建的好方法,例如:

I'm trying to create a "game", it's just to learn, and I'd like to know how would be a good way to create with JPA like a study game, for instance :

我在主目录中有这些类/表

I have these classes / tables in main

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

但是到那时,我想拥有一个存储所有这些问题的存储库,因此,当用户想要学习一点点内容时,只需从该存储库中获取问题即可.

But then I'm at the point that I'd like to have like a repository to store all of those questions, so when the user want to study a little bit just get questions from that repository.

例如,主题和子主题的目的是在用户想要提问时将其过滤掉.

The goal of the topic and the subtopic is for the filter out then when user want questions, for instance.

问题:什么是加入? 主题将是数据库 子主题为加入"

Question : What's a Join? Topic would be Database Subtopic would be Joins

您能指导我如何进行此操作吗?

Could you guide me how to follow with this?

我的问题课的例子

@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;
}

我还看到我无法创建"TRUE/FALSE","YES/NO","Small description","MULTI-CHOICE"之类的答案?

Also I'm seeing that I can not create answer like "TRUE/FALSE", "YES/NO", "Small description","MULTI-CHOICE" how do I deal with it?

推荐答案

因此,Topic可以是自引用"实体,即可以具有可选的父主题和可选的子主题集合.

So Topic can be a 'self-referencing' entity i.e. can have an optional parent topic and an optional collection of sub-topics.

主题可以嵌套到任何级别:TopicA> TopicA_1> Topic_A_1_1等.

Topics can be nested to any level: TopicA > TopicA_1 > Topic_A_1_1 etc.

通过在Topic中编写递归函数,我们可以在树中走动,并在层次结构中任何级别的Topic上获取该主题及其所有子主题的问题.

By writing a recursive function in Topic we can walk the tree and for a Topic at any level in the hierarchy get the questions for that Topic and all its sub-topics.

主题:

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;
}

因此,通过引用某个主题,我可以只获取其直接问题,也可以获取其所有问题以及所有子主题(及其所有子主题.....)的问题

So with a reference to a topic I can get either its direct questions only or all its questions plus the questions for all its sub-topics (and all their sub-topics.....)

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(); 

这篇关于Spring JPA创建问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆