2013-07-27 35 views
1

定义:休眠:删除的对象将被重新保存通过级联

@OneToMany(mappedBy = "product", fetch = FetchType.LAZY, cascade = { CascadeType.ALL }, orphanRemoval = true) 
@Where(clause = "ownerType=22") 
private List<AirlineSupplierProductFile> files = new ArrayList<AirlineSupplierProductFile>(0); 

代码:

@RequestMapping(value = "/remove-product-file", method = RequestMethod.GET, headers = BaseController.AJAX_HEADER) 
public @ResponseBody JSONResponse removeProductFile(@RequestParam(value = "id", required = true) Long id, 
     @RequestParam(value = "product", required = true) Long productId, 
     @CurrentUser UserDetailsExtended currentUser) { 
    JSONResponse json = new JSONResponse(); 

    try { 
     AirlineSupplierProductFile file = (AirlineSupplierProductFile) fileStorageService.get2(id, OwnerType.AirlinesSupplierProduct); 
     if (file.getProduct() == null || file.getProduct().getId() == productId) 
      fileStorageService.delete(file); 
    } 
    catch (Exception e) { 
     json.setData(I18n.getString("errors.common.unexepected")); 
     json.setCode(AjaxError.Undefined); 

     log(e, currentUser.getUsername()); 
    } 

    return json; 
} 

其中fileStorageService.delete(file)是:

@Transactional 
public void delete(IFileStorageFile object) { 
    Session session = SessionFactoryUtils.getSession(sessionFactory, false); 

    session.delete(object); 
} 

问题:失败deleted object would be re-saved by cascade

问题:为什么?

谢谢

+0

这能否帮助? http://stackoverflow.com/questions/15820491/org-hibernate-objectdeletedexception-deleted-object-would-be-re-saved-by-cascad –

+0

@JtheRocker没有。这个问题,你可以看到我没有任何列表来删除对象...我从db中填充对象并执行删除命令。 – nKognito

+0

这可能是由于AirlineSupplierProductFile文件仍然在调用delete(file)时引用了文件对象。尝试将文件对象复制到不同的AirlineSupplierProductFile对象,并使用它从session中删除。像delete(copied_file_obj); –

回答

0

从拥有它的产品的airlineSupplierProductFiles的列表中删除AirlineSupplierProductFile:

AirlineSupplierProductFile file = (AirlineSupplierProductFile) fileStorageService.get2(id, OwnerType.AirlinesSupplierProduct); 

if (file.getProduct() == null || file.getProduct().getId() == productId) { 
    if (file.getProduct() != null) { 
     file.getProduct().removeAirlineSupplierProductFile(file); 
    } 
    fileStorageService.delete(file); 
} 
+0

这是非常奇怪的,我有很多其他实体具有相同的关系,并在删除过程中一切正常..也许有可能手动分离该实体适合调用'删除'? – nKognito

相关问题