简体   繁体   English

Java Hibernate-是否在Hashmap中保留Hashmap?

[英]Java Hibernate - Persisting Hashmaps within Hashmaps?

Context: I want users to take an employee screening test where each question is "paired-up" with another question, but the questions are worded differently. 上下文:我希望用户参加一个员工筛选测试,其中每个问题与另一个问题都“配对”,但是问题的措词不同。

The HashMap below in the User Class only works if it's: 仅在以下情况下,用户类中的HashMap才有效:

Map<Question, Answer> answers = new HashMap<>();

However, my program would have two different answers going to two similar questions. 但是,我的程序对两个类似的问题会有两个不同的答案。 Any advice as to how I should set up my classes? 关于如何设置课程的任何建议? Would trying to persist a HashMap of a HashMap make sense? 尝试保留HashMap的HashMap是否有意义? Should I have a separate class for MatchingAnswers? 我应该为MatchingAnswers开设单独的课程吗?

Thanks 谢谢

@Entity
public class User {

    @Id
    @GeneratedValue
    int id;    

    @OneToMany(mappedBy = "user")
    @MapKeyJoinColumn(name="QUESTION_ID")
    Map<Map<Question, QuestionMatch>,Answer> answers = new HashMap<>();
}

.

@Entity
public class Question {

    @Id
    @GeneratedValue
    private int id;

    @NotNull
    @Size(min = 3, message = "Please add a question")
    private String question1;

    public Integer desiredAnswer1;

    private Boolean matchingOpposite;

    @OneToOne //not sure if this is necessary
    QuestionMatch matchingQuestion; 

.

@Entity
public class Answer {


    @Id
    @GeneratedValue
    private long id;

    private int answer;

    private int matchingAnswer;

    @ManyToOne
    private User user;

    @ManyToOne
    @JoinColumn(name="QUESTION_ID")

    Question question;

.

@Entity
public class QuestionMatch {

    @Id
    @GeneratedValue
    private int id;

    @NotNull
    @Size(min=3, message = "Please add a question")
    private String questionMatch;


    public int desiredAnswerForQuestionMatch;

I'd suggest not using such complicated structures in your entity class. 我建议不要在您的实体类中使用如此复杂的结构。 If you have a canonical Question for similar Questions you can add 如果您有类似问题的规范问题,则可以添加

@OneToOne
Question canonical;

You can create a more suited structure for you in upper layer (like Service). 您可以在上层为您创建更合适的结构(例如Service)。

You also can create a service method that finds similar questions or canonical question. 您还可以创建一种服务方法来查找类似问题或规范问题。 So before add new question, you check if similar question already exists. 因此,在添加新问题之前,请检查是否已存在类似问题。

Another option is to allow different questions have the same answer. 另一种选择是允许不同的问题具有相同的答案。 If you don't want to have the same answer be multiple time in the same test just find questions with different answers. 如果您不想获得相同的答案,请在同一测试中多次进行,只需查找具有不同答案的问题即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM