title: Spring: Save one form data into 2 entity objects
body: <p>I am developing a spring MVC application. This is an e-learning application, and the form helps the user to input multiple choice questions and its option choices. I store the question and the options in 2 different tables, question and question_answer. I have created them as an entity bean. </p>
  
  <p>I want to have one form which will take in both the questions and the answers on the same screen, but after clicking Submit, the data should be stored in two different entity beans. </p>
  
  <p>How do I achieve this? I am able to declare one commandName for an object, but ideally, I would need two command names. </p>
  
  <p>Thanks for the help!</p>
  
  <p>Question Entity:</p>
  
  <pre><code>@Entity
  @Table(name="question")
  public class Question {
  
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = "questionid", nullable=false, unique=true)
  private int questionid;
  
  @Column(name = "questiontext", nullable=false)
  private String questiontext;
  
  @Column(name = "questionstem", nullable=false)
  private String questionstem;
  
  @Column(name = "questionfeedback", nullable=false)
  private String questionfeedback;
  
  @Column(name = "subject", nullable=false)
  private String subject;
  
  @Column(name = "category", nullable=false)
  private String category;
  </code></pre>
  
  <p>Question Answer Entity</p>
  
  <pre><code>@Entity
  @Table(name="question_answers")
  public class QuestionAnswer{
  
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name="id", nullable=false, unique=true)
  private int answerid;
  
  @ManyToOne
  @JoinColumn(name="questionid")
  private int questionid;
  
  @Column(name="option1", nullable=false)
  private String option1;
  
  @Column(name="option1score", nullable=false)
  private String option1score;
  
  @Column(name="option1feedback", nullable=false)
  private String option1feedback;
  
  @Column(name="option2", nullable=false)
  private String option2;
  
  @Column(name="option2score", nullable=false)
  private String option2score;
  
  @Column(name="option2feedback", nullable=false)
  private String option2feedback;
  
  @Column(name="option3", nullable=false)
  private String option3;
  
  @Column(name="option3score", nullable=false)
  private String option3score;
  
  @Column(name="option3feedback", nullable=false)
  private String option3feedback;
  
  @Column(name="option4", nullable=false)
  private String option4;
  
  @Column(name="option4score", nullable=false)
  private String option4score;
  
  @Column(name="option4feedback", nullable=false)
  private String option4feedback;
  
  @Column(name="option5", nullable=false)
  private String opttion5;
  
  @Column(name="option5score", nullable=false)
  private String option5score;
  
  @Column(name="option5feedback", nullable=false)
  private String option5feedback;
  </code></pre>
  
  <p>Form:</p>