我正在使用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";
}
}
答案 0 :(得分:0)
不应该行
genericDAO.saveOrUpdate(entity);
对此负责吗?