2012-08-25 6 views
-1

스프링 MVC 및 JPA를 사용하고 있습니다. 나는 양식을 확인 중입니다. 그러나 유효성 검사를하는 동안 데이터가 데이터베이스로 업데이트됩니다. 왜 이런 일이 일어나는 지 이해하지 못합니다. 아무도 왜 유효성 검사 도중 데이터가 업데이트되고 있는지 말해 줄 수 있습니까?유효성 검사 도중 데이터가 업데이트되는 이유

@Override 
public String save(HttpServletRequest request, 
     @ModelAttribute("modelObject") Ticket entity, 
     BindingResult bindingResult, ModelMap model) { 
    String type = request.getParameter("type"); 

    request.getSession().setAttribute("subcontractor", entity.getSubcontractor()); 
    request.getSession().setAttribute("truck", entity.getVehicle()); 
    request.getSession().setAttribute("trailer", entity.getTrailer()); 
    request.getSession().setAttribute("terminal", entity.getTerminal()); 
    request.getSession().setAttribute("enteredBy", entity.getCreatedBy()); 
    request.getSession().setAttribute("batchDate", entity.getBillBatch()); 

    // Validate for Duplicate OriginTicket# 
    if (type.equals("complete")) { 
     // validate entity 
     try { 
      getValidator().validate(entity, bindingResult); 
     } catch (ValidationException e) { 
      e.printStackTrace(); 
      log.warn("Error in validation :" + e); 
     } 
     if (entity.getDriver() == null) { 
      bindingResult.rejectValue("driver", "error.select.option", 
        null, null); 
     } 
     if (entity.getVehicle() == null) { 
      bindingResult.rejectValue("vehicle", "error.select.option", 
        null, null); 
     } 
     if (entity.getOrigin() == null) { 
      bindingResult.rejectValue("origin", "error.select.option", 
        null, null); 
     } 
     if (entity.getDestination() == null) { 
      bindingResult.rejectValue("destination", "error.select.option", 
        null, null); 
     } 
     if (entity.getTrailer() == null) { 
      bindingResult.rejectValue("trailer", "error.select.option", 
        null, null); 
     } 
     if (entity.getCreatedBy() == null) { 
      bindingResult.rejectValue("createdBy", "error.select.option", 
        null, null); 
     } 
     if (entity.getUnloadDate()!=null && entity.getLoadDate()!=null) { 
      if (entity.getUnloadDate().before(entity.getLoadDate())) { 
       bindingResult.rejectValue("unloadDate", "error.textbox.unloadDate", 
         null, null); 
      } 
     } 
     if(reportService.checkDuplicate(entity,"O")){ 
      bindingResult.rejectValue("originTicket", "error.duplicate.entry", null, null);   
     } 
     if(reportService.checkDuplicate(entity,"D")){ 
      bindingResult.rejectValue("destinationTicket", "error.duplicate.entry", null, null);   
     } 
     /*if (getUser(request).getBillBatchDate()!=null) { 
      entity.setBillBatch(getUser(request).getBillBatchDate()); 
     }*/ 
     entity.setStatus(1); 
     if(entity.getTicketStatus()!=2){ 
      System.out.println("\nentity.getTicketStatus()!=2\n"); 
     entity.setTicketStatus(1); 
     } 

     // return to form if we had errors 
     if (bindingResult.hasErrors()) { 
      setupCreate(model, request); 
      return urlContext + "/form"; 
     } 
     beforeSave(request, entity, model); 
     User user=genericDAO.getById(User.class,entity.getCreatedBy()); 
     entity.setEnteredBy(user.getName()); 
     // merge into datasource 
     genericDAO.saveOrUpdate(entity); 
     cleanUp(request); 
     // return to list 
     setupCreate(model, request); 
     request.getSession().setAttribute("msg", 
       "Ticket added successfully"); 
     return "redirect:create.do"; 
    } else { 
     if(reportService.checkDuplicate(entity,"O")){ 
      bindingResult.rejectValue("originTicket", "error.duplicate.entry", null, null);   
     } 
     else if(reportService.checkDuplicate(entity,"D")){ 
      bindingResult.rejectValue("destinationTicket", "error.duplicate.entry", null, null);   
     } 
     // return to form if we had errors 
     if (bindingResult.hasErrors()) { 
      setupCreate(model, request); 
      return urlContext + "/form"; 
     } 
     //entity.setBillBatch(getUser(request).getBillBatchDate()); 

     //entity.setTicketStatus(0); 
     entity.setStatus(3); 
     if(entity.getTicketStatus()!=2){ 
      System.out.println("\nNEXT entity.getTicketStatus()!=2\n"); 
     entity.setTicketStatus(0); 
     } 
     beforeSave(request, entity, model); 
     User user=genericDAO.getById(User.class,entity.getCreatedBy()); 
     entity.setEnteredBy(user.getName()); 

     genericDAO.saveOrUpdate(entity); 
     return "redirect:create.do"; 
    } 
} 

답변

0

이 라인

genericDAO.saveOrUpdate(entity); 

가 책임을 안 : 여기

내 코드?

+0

저장 전 또는 업데이트하기 전에 양식 유효성 검사를하고 있지만 필드가 비어 있으면 유효성 검사 작업이 수행되지만 데이터는 데이터베이스 값으로 업데이트됩니다 .. – kkh

+0

데이터가 중복되는 항목을 확인하기 때문에 유효성 검사 중에도 업데이트되는 경우 서비스 클래스에서 메소드가 트랜잭션 클래스에 있으므로 레코드가 먼저 갱신되고 컨트롤러 클래스로 리턴 됨 – kkh

관련 문제