2017-01-03 6 views
0

学生を追加/更新しようとしていますが、学生を更新中にエラーが発生します。しかし、学生を追加する間、それはうまく動作します。更新中にこのエラーが発生します。 -HTTPステータス500 - 要求の処理に失敗しました。ネストされた例外はorg.hibernate.exception.ConstraintViolationExceptionです:ステートメントを実行できませんでした

HTTPステータス500 - 要求処理に失敗しました。ネストされた例外はorg.springframework.dao.DataIntegrityViolationExceptionです:ステートメントを実行できませんでした。 SQL [n/a];制約[null]。ネストされた例外はorg.hibernate.exception.ConstraintViolationExceptionです:文

追加-students.jspを

<form:form action="addStudent" enctype="multipart/form-data" modelAttribute="addstd" method="POST" > 
    <form:hidden path="id" /> 
    ${message} 
    <form:errors path="firstName" cssClass="error" /> 
    <form:input path="firstName" placeholder="Fistname" /> 

    <form:errors path="lastName" cssClass="error" /> 
    <form:input path="lastName" placeholder="Lastname" /> 

    <form:input path="contact_No" placeholder="Contact Number" /> 
    <form:input path="address" placeholder="Address" /> 

    <form:errors path="email" cssClass="error" /> 
    <form:input path="email" placeholder="Email" /> 

    <p class="msg"> 
     Year: 
    <form:select path="year"> 
      <c:forEach var="temp" items="${studentyear}"> 
      <form:option value="${temp.yearId}">${temp.year}</form:option> 
      </c:forEach> 
     </form:select> 

     Faculty: 
     <form:select path="faculty"> 
      <c:forEach var="temp" items="${studentfaculty}"> 
      <form:option value="${temp.faculty_id}" >${temp.faculty}</form:option> 
      </c:forEach> 
     </form:select> 
     Profile: <input type="file" name="image" accept="image/*" /> 

    </p> 
    <input type="submit" value="Add/Update Record" class="button" /> 
</form:form> 

@Controllerクラス

@RequestMapping(value="/addStudent",method=RequestMethod.POST) 
public String saveStudent(@RequestParam("image") MultipartFile file,@RequestParam("id") int theId,@ModelAttribute("addstd") @Valid StudentInfo theStudent,BindingResult result,Model model){ 
    String fileName=null; 
    if(!file.isEmpty()){ 

     try { 
      String path= session.getServletContext().getRealPath("/resources/images"); 
      String newName=String.valueOf(new java.util.Date().getTime()); 
      fileName=file.getOriginalFilename(); 
      String ext=FilenameUtils.getExtension(fileName); 
      if(ext.equalsIgnoreCase("jpg") || ext.equalsIgnoreCase("jpeg") || ext.equalsIgnoreCase("png")){ 
      File imageFile=new File(path,newName+"."+ext); 
      file.transferTo(imageFile); 
      theStudent.setImages(newName+"."+ext); 

      if(theId!=0){ 
       StudentInfo std=studentService.getStudent(theId); 
       String images= std.getImages(); 
       File oldImage=new File(path,images); 
      Files.delete(oldImage.toPath()); 
      } 
      } 
     } catch (Exception e) { 

     } 
    } 
    if(result.hasErrors()){ 


    List <Year> theYear = studentService.getYear(); 
    model.addAttribute("studentyear",theYear); 

    List<Faculty> theFaculty=studentService.getFaculty(); 
    model.addAttribute("studentfaculty",theFaculty); 
     return "add-students"; 
    }else{ 
     studentService.saveStudent(theStudent); 
     return "redirect:/login"; 

     } 
} 

@RequestMapping("/showFormForUpdate") 
public String showUpdateStudent(@RequestParam("studentId") int theId, Model model){ 

    StudentInfo theStudent=studentService.getStudent(theId); 
    model.addAttribute("addstd",theStudent); 

    List <Year> theYear = studentService.getYear(); 
    model.addAttribute("studentyear",theYear); 

    List<Faculty> theFaculty=studentService.getFaculty(); 
    model.addAttribute("studentfaculty",theFaculty); 
    return "add-students"; 
} 

StudentDAOImpl.class

public void saveStudent(StudentInfo theStudent) { 
    Session currentSession=sessionFactory.getCurrentSession();  
    currentSession.saveOrUpdate(theStudent); 

} 
を実行することができませんでした

StudentInfo.class

あなたのStudentInfoクラスで
@Entity 
@Table (name="studentinfo") 
public class StudentInfo implements Serializable { 

@Id 
@GeneratedValue(strategy = GenerationType.IDENTITY) 
@Column(name="id") 
private int id; 


@Column(name="year_id") 
private int year; 


@Column(name="faculty_id") 
private int faculty; 

@NotEmpty(message="First Name cannot be empty") 
@Column(name="firstname") 
private String firstName; 

@NotEmpty(message="Last Name cannot be empty") 
@Column(name="lastname") 
private String lastName; 

@Column(name="contact_no") 
private String contact_No; 

@Column(name="address") 
private String address; 


@Email(message="Enter a valid email address") 
@Column(name="email") 
private String email; 


@Column(name="images") 
private String images; 

@OneToOne(fetch = FetchType.LAZY) 
@JoinColumn(name="ID") 
private User user; 

//getter and setter here 

User.class

@Entity 
@Table(name="user") 
public class User { 

@Id 
@GeneratedValue(strategy = GenerationType.IDENTITY) 
@Column(name="user_id") 
private int user_id; 

@Column(name="username") 
private String username; 

@Column(name="password") 
private String password; 

@OneToOne(mappedBy = "user",fetch = FetchType.LAZY) 
private StudentInfo info; 

//getter and setter here 
+0

データの更新によっていくつかのデータベース制約が違反していますか? –

+0

[スタックトレースとは何ですか?また、アプリケーションのエラーをデバッグするためにどうすれば使用できますか?](http://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how -i-use-it-to-debug-my-application-errors)を使用すると、 – Raedwald

答えて

0

つのフィールドがあります:

private User user; 

あなたは、フォームのコントローラで任意のフィールドをユーザにマッピングされていません。

あなたはこのようなあなたのユーザーをマッピングすることができます

<form:hidden path="user.user_id"/> 

あなたはヌルとしてこの値を許可する場合は、あなたの一対一の注釈で

nullable = true 

を提供し、また中にはnullが可能db。

あなたが学生を追加していて、あなたがヌル入力可能でない場合は、何らかの形であなたのコントローラにユーザーを注入する必要があります。

たとえば、新しいStudentInfoを追加している間にコントローラメソッドでデータベースからユーザー情報を取得し、studentInfoに挿入します。私は以下のようにsudoのコードを書いています:

// User user = session.get(User.class, 1); 
// studentInfo.setUser(user); 
// saveorUpdate studentInfo 
関連する問題