I am using Hibernate and Spring MVC. I have the following form
When I submit the form I want my database tables to fill up like following:
Table Name : student
student_id studentName
1. Jason Stathum
Table Name : studentdetails
studentDetailsid FatherName MotherName city_id student_id
1 Mr.X Mrs. Y 1 1
But when I actually run my application and insert the form data, everything works perfectly except the column city_id
do not get filled up. The table looks like following:
Table Name : studentdetails
studentDetailsid FatherName MotherName city_id student_id
1 Mr.X Mrs. Y NULL 1
FYI, I have also a table named city which looks like following
Table Name : studentdetails
city_ID city_name
1 NewYork
I have included my codes below, Could you please tell me how to fix this problem?
Thank you very much
Entity: Student
@Entity
@Table(name = "student")
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="student_id", nullable= false)
private Integer studentId;
private String studentName;
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name="student_id", referencedColumnName="student_id")
private List<StudentDetails> studentDetails = new ArrayList<StudentDetails>();
// Getters and Setters
Entity : StudentDetails
@Entity
@Table(name = "studentDetails")
public class StudentDetails {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer studentDetailsId;
private String FatherName;
private String MotherName;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name="city_id")
private City city;
// Getters and Setters
Entity: City
@Entity
@Table(name = "CITY")
public class City {
@Id
@Column(name="city_ID")
@GeneratedValue
private int cityId;
@Column(name="city_name")
private String cityName;
// Getters and Setters
Controller
@RequestMapping(value="addstudent", method = RequestMethod.GET)
public String addStudent(@ModelAttribute("secret") Student student, BindingResult result, Model map)
{
map.addAttribute("student", new Student());
List<City> myCities = subService.getCity();
map.addAttribute("city", myCities);
return "addStudent";
}
@RequestMapping(value="addstudent", method = RequestMethod.POST)
public String saveStudent(@ModelAttribute("secret") Student student, BindingResult result, Model map)
{
studentService.addStudent(student);
map.addAttribute("success", "Submitted");
return "msg";
}
StudentDaoImpl
Session session = getSessionFactory().openSession();
Transaction tx;
tx = session.beginTransaction();
session.save(student);
tx.commit();
My Form : addStudent.jsp
<c:url var="saveUrl" value="/addstudent" />
<form:form modelAttribute="secret" method="POST" action="${saveUrl}">
<table>
<tr>
<td><form:label path="studentName">Student Name:</form:label></td>
<td><form:input path="studentName"/></td>
</tr>
<tr>
<td><form:label path="studentDetails[0].fatherName">Father Name:</form:label></td>
<td><form:input path="studentDetails[0].fatherName"/></td>
</tr>
<tr>
<td><form:label path="studentDetails[0].motherName">Mother Name:</form:label></td>
<td><form:input path="studentDetails[0].motherName"/></td>
</tr>
<tr>
<td><label>City</label></td>
<td><form:select path="studentDetails[0].city">
<c:forEach var="row" items="${city}">
<option value="${row.cityId}">${row.city}</option>
</c:forEach>
</form:select>
</td>
</tr>
</table>
<input type="submit" value="Save" />
</form:form>
I'm speculating here, but could it be that you didn't actually select a city when entering a record, but instead just entered a student name, a father name and a mother name, and left the city displayed at the first city in your list, which happened to be "New York". Perhaps you could add an extra "please choose" option, with a validator that forces you to choose a city.
Also, I notice that you use city_id in some places and city_ID in others. Is your database case-insensitive for column names?
I have manged to solve the problem :) I made some changes in my HTML form and now its working.
<tr>
<td><label>City</label></td>
<td><form:select path="studentDetails[0].city.cityId">
<c:forEach var="row" items="${city}">
<option value="${row.cityId}">${row.cityName}</option>
</c:forEach>
</form:select>
</td>
</tr>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.