Spring:将一个表单数据保存到2个实体对象中

时间:2014-06-30 10:56:16

标签: java spring java-ee spring-mvc

我正在开发一个spring MVC应用程序。这是一个电子学习应用程序,该表单可帮助用户输入多项选择题及其选项。我将问题和选项存储在2个不同的表,question和question_answer中。我已经将它们创建为实体bean。

我希望有一个表单可以同时在同一个屏幕上显示问题和答案,但是在点击提交后,数据应该存储在两个不同的实体bean中。

我如何实现这一目标?我能够为一个对象声明一个commandName,但理想情况下,我需要两个命令名称。

感谢您的帮助!

问题实体:

@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;

问题解答实体

@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;

形式:

1 个答案:

答案 0 :(得分:0)

我觉得你应该为问题和答案(选项)创建单个bean,使用一个commandName对象,然后在服务层中将它们分隔为问题和答案(选项),并在dao层中将它们保存在各自的表表中(问题和question_answer),请把它作为一个建议,因为这不是你的问题的答案它的解决方法,但以适当的方式;),谢谢

public class QuestionAnswers{

private int questionid;

private String questiontext;
private String questionstem;
private String questionfeedback;
private String subject;
private String category;

private int answerid;

private String option1;
private String option1score;
private String option1feedback;


private String option2;
private String option2score;
private String option2feedback;


private String option3;
private String option3score;
private String option3feedback;


private String option4;
private String option4score;
private String option4feedback;


private String opttion5;
private String option5score;
private String option5feedback;

}