2014-03-31 31 views
2

我有一个MVC应用程序创建新的办公室,而不是在使用编辑表单时更新它们。请帮我理解为什么会发生这种情况。Spring MVC,尝试编辑但创建新对象

用于填充搜索结果的搜索方法:

@RequestMapping(value = "/searchResults", method = RequestMethod.POST) 
public ModelAndView search(@RequestParam String searchCriteria, HttpServletRequest request) { 
    List<Office> offices = officeServiceImpl.search(searchCriteria); 
    return new ModelAndView("searchResults", "offices", offices); 
} 

这里是链接编辑表单看起来像搜索结果页上:

<a href="${pageContext.request.contextPath}/app/office/${office.getId()}/edit" class="btn btn-success">Edit Office</a> 

这里是控制器的编辑GET方法用现有的办公室填写表格:

@RequestMapping(value = "/{officeId}/edit", method = RequestMethod.GET) 
@Transactional(noRollbackFor=NoResultException.class) 
public ModelAndView initUpdateOfficeForm(
    @PathVariable("officeId") Long officeId, Model model) { 
    Office office = officeServiceImpl.find(officeId); 
    //prepareEditFormModelAndView(office) just converts some objects to strings for typeahead form population 
    return prepareEditFormModelAndView(office); 
} 

这里是编辑POST me的ThOD:

@RequestMapping(value = "/{officeId}/edit", method = RequestMethod.POST) 
public ModelAndView processUpdateOfficeForm(@ModelAttribute("office") @Valid Office office, 
     BindingResult result, SessionStatus status) { 
    if (! "united states of america".equals(office.getFolderStrings().toLowerCase())) { 
     //This portion of code converts the typeahead strings to objects 
     result = tryCountries(office, result); 
     result = tryDepartments(office, result); 
     result = tryEmployees(office, result); 
     } 
     if (result.hasErrors()) { 
     return prepareEditFormModelAndView(office); 
    } else { 
     officeServiceImpl.save(office); 
     status.setComplete(); 
     return new ModelAndView("editResult", "office", office); 
    } 
} 

officeServiceImpl调用officeRepositoryImpl方法保存它看起来像:

@Override 
public Office save(Office office) { 
    em.merge(office); 
    em.flush(); 
    return office; 
} 

感谢

编辑:添加prepareEditFormModelAndView(办公室),此方法试图从关联对象建立字符串:

@Transactional(noRollbackFor={NoResultException.class, IndexOutOfBoundsException.class}) 
private ModelAndView prepareEditFormModelAndView(Office office) { 
    String departmentStrings = ""; 
    String employeeStrings = ""; 

    List<OOM> officeOOMs = new ArrayList<OOM>(); 
    StringBuilder sb = new StringBuilder(); 
    try { 
     officeOOMs = oomServiceImpl.getOOMsForCurrentOffice(office.getId()); 
    } catch (NoResultException e) { 
     officeOOMs = null; 
    } 
    for (OOM o : officeOOMs) { 
     try { 
      Employee tempEmployee = employeeServiceImpl.find(o 
        .getEmployeeId()); 
      sb.append(tempEmployee.getDisplayName() + ", "); 
     } catch (NoResultException e) { 
      sb.append("Not found in system"); 
     } 
    } 
    employeeStrings = sb.toString(); 

    if ((! "".equals(office.getDepartmentStringsOnForm())) && office.getDepartmentStringsOnForm() != null) { 
     departmentStrings = office.getDepartmentStringsOnForm(); 
    } 
    String folderStrings = ""; 
    try { 
     folderStrings = kmlFolderServiceImpl.getInternationalOfficeString(office.getId()); 
     LOGGER.info("Folder Strings: " + folderStrings); 
    } catch (NoResultException e) { 
     folderStrings = ""; 
     LOGGER.info("Folder Strings: " + "no result"); 
    } 
    boolean isInternational = office.isInternational(); 
    ModelAndView result = new ModelAndView("editOfficeForm", "office", office); 
    result.addObject("departmentStrings", departmentStrings); 
    result.addObject("isInternational", isInternational); 
    result.addObject("folderStrings", folderStrings); 
    result.addObject("employeeStrings", employeeStrings); 
    return result; 
} 
+0

prepareEditFormModelAndView做什么? –

+0

当你在邮局工作时,你也发送它的ID吗? – geoand

+0

@geoand,不,我没有通过身份证。我通过了办公室的模型属性。 –

回答

2

我在添加以前的评论在这里,为了更好的澄清。根据OP,以下问题修复了以下问题:

当ID不在表单中时,当模型返回时,没有ID设置为使持久性提供者认为它是新实体的实体。 因此,最明显的解决方案是在保存操作中发布实体的ID(可能使用隐藏字段)。

另一种解决方案是尝试基于某个业务密钥 加载数据库中的实体以查看实体是否为新实体。