如何在Hibernate中实现Composite Primary Key?

时间:2018-07-28 18:25:40

标签: java jpa

我有两个带有主键QuestionNumber和assessmentId的表。我想使用这两个使它们在两个不同的表中组合。这可能吗。我应该在此添加一些东西使其工作。或有另一种方法来实现它。或者我应该将问题元数据添加到问题中,并只在AnswerKey中使用复合键

  Question

        @Entity
        @Getter
        @Setter
        public class Question implements Serializable {

            @Id
            @GeneratedValue(strategy = GenerationType.AUTO)
            private int questionId;

            private int questionNumber;

            private String assessmentId;

            private QuestionTypes questionType;

            private String questionText;

            private String questionURL;

            private QuestionStatus questionStatus;

            @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
            @JoinColumn(name = "questionNumber", referencedColumnName = "questionNumber")
            private List<QuestionOption> questionOptions;
           //Constructor
        // Getter and setter
        }

Assessment

@Entity
@JsonIgnoreProperties({"questionList"})
public class Assessment {

    @Id
    private String assessmentId;

    private String assessmentTopic;

    private String assessmentSubTopic;

    private String assessmentLevel;

    private String createdBy;

    private String rating;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "assessmentId", fetch = FetchType.LAZY)
    private List<Question> questionList;

   //Constructor
   //Setter and getter
}


QuestionMetaData



     @Entity
        public class QuestionMetaData {

            @Id
            private QuestionAssessmentKey questionAssessmentKey;

            private String topicName;

            private String subtopicName;

            private QuestionComplexity complexity;

            private String conceptName;
           //Getter and setter
          //Constructor
        }

   AnswerKey



@Entity
public class AnswerKey {

    @Id
    private QuestionAssessmentKey questionAssessmentKey;

    private Character answer;

  //Constructor
// Setter and getter
}


    Key

    @Embeddable
    public class QuestionAssessmentKey implements Serializable {

        private int questionNumber;

        private String assessmentId;
      //Constructor
    //Setter and Getter
    }

1 个答案:

答案 0 :(得分:1)

这些是“派生身份”,因此您的映射应如下所示:

@Entity
public class Assessment {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private String assessmentId;

    private String assessmentTopic;

    private String assessmentSubTopic;

    private String assessmentLevel;

    private String createdBy;

    private String rating;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "assessment", fetch = FetchType.LAZY)
    private List<Question> questionList;

    // ...
}

@Entity
public class Question implements Serializable {

    @EmbeddedId
    private QuestionAssessmentKey questionAssessmentKey;

    @ManyToOne
    @MapsId("assessmentId") // maps assessmentId attribute of embedded id
    private Assessment assessment;

    private QuestionTypes questionType;

    private String questionText;

    private String questionURL;

    private QuestionStatus questionStatus;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinColumn(name = "questionNumber", referencedColumnName = "questionNumber")
    private List<QuestionOption> questionOptions;

    // ...
}

@Entity
public class QuestionMetaData {

    @EmbeddedId
    private QuestionAssessmentKey questionAssessmentKey;

    private String topicName;

    private String subtopicName;

    private QuestionComplexity complexity;

    private String conceptName;

    @ManyToOne
    @MapsId("assessmentId") // maps assessmentId attribute of embedded id
    private Assessment assessment;

    // ...
}

@Entity
public class AnswerKey {

    @EmbeddedId
    private QuestionAssessmentKey questionAssessmentKey;

    private Character answer;

    @ManyToOne
    @MapsId("assessmentId") // maps assessmentId attribute of embedded id
    private Assessment assessment;

    // ...
}

@Embeddable
public class QuestionAssessmentKey implements Serializable {

    private String assessmentId;

    private int questionNumber;

    // ...
}