Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 343447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T10:53:54+00:00 2026-05-12T10:53:54+00:00

I’m not too used to ORM mapping, having come from a PHP background. I’m

  • 0

I’m not too used to ORM mapping, having come from a PHP background. I’m trying to achieve a link between a series of questions and their possible answers, along with the correct answer from that set.

I’m trying to create a Question object that can be queried for it’s corresponding Answers, but unfortunately, the best Java classes I’ve crafted lead to duplicate (technically unneccessary) fields.

The ideal database schema would look like this:

CREATE TABLE `question` (
    `question_id`           BIGINT AUTO_INCREMENT,
    `questionText`          VARCHAR(255),
    `correctAnswerIndex`    INT,
    PRIMARY KEY (`question_id`),
    INDEX correct_answer_idx (`question_id`, `correctAnswerIndex`),
    FOREIGN KEY `correct_answer_idx` (`question_id`, `correctAnswerIndex`) REFERENCES answer(`question_id`, `index`)
);

CREATE TABLE `answer` (
    `question_id`           BIGINT,
    `index`                 INT,
    `answerText`            VARCHAR(32),
    PRIMARY KEY (`question_id`, `index`),
    INDEX question_idx (`question_id`),
    FOREIGN KEY `question_idx` (`question_id`) REFERENCES `question`(`question_id`)
);

Unfortunately, the Java classes I’m about to list will create an extra set of (question_id, index) for the Map. This would be acceptable if I could use the fields as the primary key.
In addition, correctAnswer will generate an unneccessary reference to Question when using the current id would be fine.

The following are the classes that I’m using to create the structure:

@javax.persistence.Entity
@javax.persistence.Table(name = "question")
public class Question implements Serializable {
    @Id
    @TableGenerator(name = "question_seq", table = "hibernate_sequences", allocationSize = 5)
    @GeneratedValue(strategy = GenerationType.TABLE, generator = "question_seq")
    private Long id;

    @ManyToOne(optional = false)
    @JoinColumns({
        @JoinColumn(name = "correct_answer_index", referencedColumnName = "index"),
        @JoinColumn(name = "correct_answer_question", referencedColumnName = "question_id")
    })
    private Answer correctAnswer;

    @CollectionOfElements(fetch = FetchType.LAZY)
    @JoinTable(name = "answer", joinColumns = @JoinColumn(name = "question_id"))
    @MapKey(columns = { @Column(name = "index") })
    private Map<Integer, Answer> answers = new HashMap<Integer, Answer>();

    @Column(length = 255)
    private String questionText;

    //... Accessors, Equals(), Hashcode(), etc...
}

@javax.persistence.Entity
@javax.persistence.Table(name = "answer")
@AssociationOverrides( {
    @AssociationOverride(name = "pk.index", joinColumns = @JoinColumn(name = "index")),
    @AssociationOverride(name = "pk.question", joinColumns = @JoinColumn(name = "question_id")),
})
public class Answer implements Serializable {
    @EmbeddedId
    private AnswerPK pk = new AnswerPK();

    @Column(length = 32)
    private String answerText;

    //... Accessors, Equals(), Hashcode(), etc...
}

@Embeddable
public class AnswerPK implements Serializable {
    @ManyToOne
    @ForeignKey(name = "answer_for_question")
    Question question;

    Integer index;

    //... Accessors, Equals(), Hashcode(), etc...
}

Any help or advice would be greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T10:53:54+00:00Added an answer on May 12, 2026 at 10:53 am

    Don’t use a Map but a List for the answers and use index as the sort order. Also assign each answer a unique ID (just like with the questions) instead of using a composite key. Hibernate will use this ID in the question object (instead of the index).

    The only drawback of this design is that you must make sure that the correct answer is in fact an item in the list of answers to this question. On the positive side, this will make your code and mapping much more simple.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 175k
  • Answers 175k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer One way to do this is to use the GINPUT… May 12, 2026 at 3:02 pm
  • Editorial Team
    Editorial Team added an answer Typically this is done by your template inheriting from an… May 12, 2026 at 3:02 pm
  • Editorial Team
    Editorial Team added an answer As far as I can remember when you use VS.net… May 12, 2026 at 3:02 pm

Related Questions

In order to apply a triggered animation to all ToolTip s in my app,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a French site that I want to parse, but am running into
I have text I am displaying in SIlverlight that is coming from a CMS

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.