1

I have two tables with primary keys questionNumber and assessmentId. I want to use these two to make them composite in two different tables. Is this possible. Should I add something to this to make it work. OR is there another way to implement it. OR should I add question meta data to question and just used the composite key in 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 Answer 1

1

These are "derived identities", so your mappings should like this:

@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; // ... } 
Sign up to request clarification or add additional context in comments.

6 Comments

In QuestionMetadat and answerkey classes u have added questionId. But shouldnt that be QuestionNumber as my composite key is questionnumber + assessment id. Not question id + assessment id.
Isnt it possible to leave questionId and have question number as part of the composite key
Hmmm. Sorry about that. I edited my answer to use questionNumber. Maybe that is more helpful?
Also, I do no understand your model perfectly; but it seems like the primary key for Question should be a composite key made up of assessmentId and questionNumber (i.e. each Question belongs to an Assessment and has a unique questionNumber within that Assessment). Does that make sense?
Yeah u r right. Its better that even that has a composite key. So should i temove questionid completely
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.