java - Hibernate:删除的对象将通过级联重新保存

标签 java spring hibernate spring-mvc

定义:

@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);
}

问题:失败,删除的对象将通过级联重新保存

问题:为什么?

谢谢

最佳答案

从其所属产品的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);
}

关于java - Hibernate:删除的对象将通过级联重新保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17894557/

相关文章:

java - 一对多 Spring 结果映射

hibernate - 使用 hibernate @NamedNativeQuery 返回数值

java - 如何在java中将16位灰度图像转换为RGB图像?

java - Java中的Elasticsearch条件评分

java - 如何更改 JtabbedPane 鼠标悬停时选项卡文本的颜色?

java - Hibernate - 如何将具有 pk 和 fk 的对象设置为数据库中的 VARCHAR?

java - spring中嵌套配置属性的前缀

java - 如何用LocalDate查询LocalDateTime?

java - 删除实体@OneToMany @ManyToOne。键仍然从表中引用

java - 如何优化周期?