私はEntityクラスのExaminationを持っており、EntityクラスのStudentとManyToOneとして接続しています。そして試験は、ManyToOne として Subject と接続されます。I JSP ファイル 対応する入力を Controller に送信しました。しかし、送信しようとすると、エラー (不正な要求) が発生します。こうなってしまう原因がわかりました。Examination には、examinationMark、student、subject、examinationId というフィールドがあります。検査IDは自動生成されます。JSP に入力するその他のフィールド。Ajaxを使用してコントローラーで学生と試験対象の値を渡そうとすると、エラーが発生します。でも、examinationMarkだけ合格すればOKです。なぜそれが起こるのかわかりません。
@Entity
@Table(name = "Examination")
public class Examination implements Serializable {
public Examination() {}
public Examination(String examinationMark) {
this.examinationMark = examinationMark;
}
// create connectivity with table Student
private Student student;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "StudentID")
public Student getStudent() {
return this.student;
}
public void setStudent(Student student) {
this.student = student;
}
// create connectivity with table Subject
private Subject subject;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "SubjectId")
public Subject getSubject() {
return subject;
}
public void setSubject(Subject subject) {
this.subject = subject;
}
サブジェクト エンティティ クラス
@Entity
@Table(name = "Subject")
public class Subject implements Serializable {
public Subject() {}
public Subject(String subjectTitle, int subjectHours) {
this.subjectTitle = subjectTitle;
this.subjectHours = subjectHours;
}
// create connectivity with table Examination
private Set<Examination> examinations;
@OneToMany(mappedBy = "subject", cascade = CascadeType.ALL, orphanRemoval = true)
public Set<Examination> getExaminations() {
return examinations;
}
public void setExaminations(Set<Examination> examinations) {
this.examinations = examinations;
}
学生エンティティ クラス
@Entity
@Table(name = "Student")
public class Student implements Serializable {
public Student() {}
public Student(String studentFullName, String studentBook,
int studentEnter, String studentOKR) {
this.studentFullName = studentFullName;
this.studentBook = studentBook;
this.studentEnter =studentEnter;
this.studentOKR = studentOKR;
}
// create connectivity with table Examination
private Set<Examination> examinations = new HashSet<Examination>();
@OneToMany(mappedBy = "student",cascade = CascadeType.ALL, orphanRemoval = true)
public Set<Examination> getExaminations() {
return examinations;
}
public void setExaminations(Set<Examination> examinations) {
this.examinations = examinations;
}
コントローラーのメソッド
@RequestMapping(value = "/studentProfileEdit.html", method = RequestMethod.GET)
public ModelAndView getStudentProfile() {
ModelAndView mav = new ModelAndView("studentProfileEdit"); // create MVC object
// to pass it to JSP page
mav.getModelMap().put("student", sts.selectStudentByName("name"));
return mav;
}
@RequestMapping(value = "/studentProfileEdit.html", method = RequestMethod.POST)
public @ResponseBody String editStudentProfile( @ModelAttribute(value = "examination") Examination examination) {
return "";
}
JSPファイル
<div id="examPart">
<label>Subject</label>
<select id="subject">
<c:forEach var="s" items="${subjects}">
<option value="${s.subjectTitle}" >${s.subjectTitle}</option>
</c:forEach>
</select>
<br/>
<label>Exam mark</label>
<input id="examinationMark" />
<input type="submit" value="Add exam" onclick="addExam()" />
<div id="exam" style="color:green"></div>
</div>
およびAjax関数
function addExam() {
var examinationMark = $('#examinationMark').val();
var subject = $('#subject');
var student = '${student}';
$.ajax({
type: "POST",
url: "/IRSystem/studentProfileEdit.html",
data: "examinationMark=" + examinationMark +
"&student=" + student +
"&subject=" + subject ,
success: function(response) {
$('#exam').html(response);
$('#examinationMark').val('');
},
error: function(e) {
alert('Error' + e);
}
});
}