Spring 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";
}
}