-1
我正在使用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";
}
}
之前保存或更新我驗證的形式..但如果字段爲空驗證工作,但數據更新到數據庫基地..與空值.. – kkh
數據獲取更新,即使驗證,因爲我檢查重複條目方法在服務類中,在服務類事務中存在,所以記錄首先更新並返回到控制器類 – kkh