2013-07-27 3 views
1

Определение:Hibernate: удаленный объект будет вновь спасена каскад

@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 Nope. Проблема, поскольку вы можете видеть, что у меня нет списка для удаления объекта из ... Я заполняю объект из db и выполняю команду delete. – nKognito

+0

это может быть связано с тем, что файл AirlineSupplierProductFile по-прежнему ссылается на файл-объект при вызове delete (file). Попробуйте скопировать объект файла в другой объект AirlineSupplierProductFile и использовать его для удаления из сеанса. Что-то вроде delete (copied_file_obj); –

ответ

0

Удалить AirlineSupplierProductFile из списка airlineSupplierProductFiles его владеющего продукта:

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

Смежные вопросы