具有嵌套实体对象的实体上的CRUD

时间:2016-12-08 11:46:33

标签: java hibernate jsp spring-mvc servlets

祝贺所有人。我是春天mvc的新人。我有一个Student实体,它有User个对象作为一对一的关系。 student-form.jsp是我创建学生的输入形式。

  

实体类:跳过详细信息。

    public class Student implements Serializable {
    private static final long serialVersionUID = 1L;
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer studentId;
    private String citizenship;
    private String nric;
    @JoinColumn(name = "user_userId", referencedColumnName = "userId")
    @OneToOne(optional = false,cascade = CascadeType.ALL)
    private User user;
}


  public class User implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer userId;
    private String email;
    private String password;
    private String phone;
    @OneToOne(cascade = CascadeType.ALL, mappedBy = "user")
    private Student student;
}
  

Spring Mvc Controller显示表单的方法。

 @RequestMapping("/student/new") //show blank student form
public ModelAndView showStudentFormNew() {
    Student s = new Student();
    User u = new User();
    s.setUser(u);
    ModelAndView v = new ModelAndView("crud/student-form");
    v.addObject("student", s);
    return v;
}
  

在表单中单击提交按钮时的控制器发布请求接受方法:

 @RequestMapping("/student/add") //add student to database
public ModelAndView addOrUpdateStudent(@ModelAttribute("student")    Student student) {

   // studentDao.save(student); // this doesnt work. 
    return getDebug(student.getUser().getFirstName()); //student.getuser() alwasy throws null pointer exception.   
}
  

N.B getDebug(string message)将调试页面返回为ModelAndView

     

因此user对象中的Student为空。即使我在传递给student-form.jsp showStudentFormNew()方法之前设置了User in Student对象,如果我取消注释//studentDao.save(student),它会给我一个约束违例异常,因为Student表没有任何UserID在其外键列中。这意味着User表中的UserId不会自动生成为自动增量。

     

student-form.jsp:页。

<body>
    <div class="container">
        <form action="${pageContext.request.contextPath}/admin/student/add" method="post"  role="form" data-toggle="validator" >


            <div class="form-group col-xs-4">
                <br></br>
                <label for="student.user.firstName" class="control-label col-xs-4">First Name:</label>
                <input type="text" name="student.user.firstName" id="student.user.firstName" class="form-control" value="${student.user.firstName}" required="true"/>                                   

                <label for="student.user.lastName" class="control-label col-xs-4">Last Name:</label>                   
                <input type="text" name="student.user.lastName" id="student.user.lastName" class="form-control" value="${student.user.lastName}" required="true"/> 

                <label for="student.user.dob" class="control-label col-xs-4">Birth Date:</label>                 
                <input type="date"  name="lecturer.user.dob" id="student.user.dob" class="form-control" value="${student.user.dob}" placeholder="dd-MM-yyy" required="true"/>

                <label for="student.user.gender" class="control-label col-xs-4">Gender:</label>
                <input type="text" name="student.user.gender" id="student.user.gender" class="form-control" value="${student.user.gender}" placeholder="male&female" required="true"/>

                <label for="student.citizenship" class="control-label col-xs-4">Citizenship:</label>                    
                <input type="text" name="student.citizenship" id="student.citizenship" class="form-control" value="${student.citizenship}" required="true"/> 

                <label for="student.nric" class="control-label col-xs-4">NRIC:</label>                   
                <input type="text" name="student.nric" id="student.nric" class="form-control" value="${student.nric}"  required="true"/>

                <br></br>
                <br></br>
                <h2>Login Information</h2>
                <br></br>
                <input type="hidden" id="student.user.userId" name="student.user.userId" value="${student.user.userId}">

                <label for="student.user.userName" class="control-label col-xs-4">User Name:</label>
                <input type="text" name="student.user.userName" id="student.user.userName" class="form-control" value="${student.user.userName}" required="true"/>

                <label for="student.user.password"  class="control-label col-xs-4">Password:</label>
                <input type="text" name="student.user.password" id="student.user.password" class="form-control" value="${student.user.password}" required="true"/>

                <label for="student.user.phone"  class="control-label col-xs-4">Phone:</label>
                <input type="text" name="student.user.phone" id="student.user.phone" class="form-control" value="${student.user.phone}" required="true"/>

                <label for="student.user.address"  class="control-label col-xs-4">Address:</label>
                <input type="text" name="student.user.address" id="student.user.address" class="form-control" value="${student.user.address}" required="true"/>

                <label for="student.user.email"  class="control-label col-xs-4">Email:</label>
                <input type="text" name="student.user.email" id="student.user.email" class="form-control" value="${student.user.email}" placeholder="student@gmail.com" required="true"/>


                <input type="hidden" name="student.user.role" id="student.user.role" class="form-control" value="student" required="true"/>

                <label for="student.user.status"  class="control-label col-xs-4">Status:</label>
                <input type="text" name="student.user.status" id="student.user.status" class="form-control" value="${student.user.status}" required="true"/>

                <br></br>

                <button type="submit" class="btn btn-primary  btn-md">Accept</button> 
            </div>
        </form>
    </div>   
</body>

0 个答案:

没有答案