gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 04:31:46 26 4
gpt4 key购买 nike

定义:

@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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com