简体   繁体   English

在 SpringBoot 中从数据库中删除项目时出错:无法删除或更新父行:外键约束失败

[英]Error when deleting item from database in SpringBoot: Cannot delete or update a parent row: a foreign key constraint fails

I have two tables, item and user, userId is a foreign key in item table using @ManyToOne mapping.我有两个表,item 和 user,userId 是使用 @ManyToOne 映射的 item 表中的外键。 I need to delete an item just by pressing a button on a bootstrap front end but I get the error: Cannot delete or update a parent row: a foreign key constraint fails ( jdbc . ms_item , CONSTRAINT FKli14y8viufmofrho0tdmgqawy FOREIGN KEY ( id ) REFERENCES ms_users ( id )).我需要通过在引导程序前端按下一个按钮来删除一个项目,但我收到错误:无法删除或更新父行:外键约束失败( jdbc ,CONSTRAINT ms_item FOREIGN KEY( idms_users FKli14y8viufmofrho0tdmgqawy id ))。 I understand that I can't delete a row from a db which has reference to another entity so Im wondering how I can delete.我知道我无法从引用了另一个实体的数据库中删除一行,所以我想知道如何删除。 Another thing to note is that update works fine!另一件需要注意的事情是更新工作正常!

Here is my code:这是我的代码:

@Controller
public class ProductController {

@Autowired
private ItemRepository itemRepository;
@Autowired
private UserRepository userRepository;

@Autowired
private ItemServiceImp itemServiceImp;

@GetMapping("/listItems")
public String listing(Model model) {
    model.addAttribute("item", new Item());
    model.addAttribute("pageTitle", "Sell Product");
    return "addItem";
}

@GetMapping("/products")
public String listItems(Model model, Principal principal) {
    User user = userRepository.findByEmail(principal.getName());
    List<Item> listItems = itemRepository.findByUser(user);
    model.addAttribute("listItems", listItems);
    return "productList";
}

@PostMapping("/products/save")
public String itemAdd(Item item, Principal principal, RedirectAttributes redirectAttributes) {
    User user = userRepository.findByEmail(principal.getName());
    item.setUser(user);
    itemRepository.save(item);
    redirectAttributes.addFlashAttribute("message", "Product Listed for sale");
    return "home_page";
}

@GetMapping("/products/update/{itemId}")
    public String updateItem(@PathVariable("itemId") Long itemId, Model model, RedirectAttributes redirectAttributes) {
    try {
        Item item = itemServiceImp.get(itemId);
        model.addAttribute("item", item);
        model.addAttribute("pageTitle", "Update Product");
        return "addItem";
    } catch (ItemNotFoundException e) {
        redirectAttributes.addFlashAttribute("message", "Product Updated");
        return "home_page";
    }

}

@GetMapping("/products/delete/{itemId}")
public String deleteItem(@PathVariable("itemId") Long itemId) {

    itemServiceImp.delete(itemId);
    return "redirect/productList";
}

}


@Entity
@Table(name = "msItem")
public class Item {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long itemId;
@Column(nullable = false, length = 45)
private String itemName;
@Column(nullable = false)
private int itemPrice;
@Column(nullable = false, length = 100)
private String itemDesc;
@Column(nullable = false, length = 100)
private String category;
@Column(nullable = false, length = 100)
private String image;
@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinColumn(name = "id")
    private User user;


@Service
public class ItemServiceImp{

@Autowired
private ItemRepository itemRepository;

public List<Item> listItems(User user) {
     return itemRepository.findByUser(user);
}

public Item get(Long itemId) throws ItemNotFoundException {
    Optional<Item> result = itemRepository.findById(itemId);

    if (result.isPresent()) {
        return result.get();
    }

    throw new ItemNotFoundException("No Item with id: " + itemId);
}

public void delete(Long itemId) {
    itemRepository.deleteById(itemId);
}
}

best practice is to have remove boolean in your document and make it true or false it cause to have soft delete instead of hard delete最佳做法是在您的文档中删除 boolean 并使其为真或假,这会导致软删除而不是硬删除

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法删除或更新父行:删除具有引用的实体时外键约束失败 - Cannot delete or update a parent row: a foreign key constraint fails when deleting entity with reference SpringBoot App 抛出 ava.sql.SQLIntegrityConstraintViolationException:无法删除或更新父行:外键约束失败 - SpringBoot App throws ava.sql.SQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails 休眠:无法删除或更新父行:外键约束失败 - Hibernate :Cannot delete or update a parent row: a foreign key constraint fails 休眠错误:无法删除或更新父行:外键约束失败 - Hibernate error:Cannot delete or update a parent row: a foreign key constraint fails Hibernate 错误:无法删除或更新父行:外键约束失败 - Hibernate ERROR: Cannot delete or update a parent row: a foreign key constraint fails 无法删除或更新父行:java jdbc 中的外键约束失败 - Cannot delete or update a parent row: a foreign key constraint fails in java jdbc com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:无法删除或更新父行:外键约束失败 - com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails Hibernate 抛出无法删除或更新父行:外键约束失败 - Hibernate throws Cannot delete or update a parent row: a foreign key constraint fails 无法删除或更新父行:Spring Boot JPA 中的外键约束失败 - Cannot delete or update a parent row: a foreign key constraint fails in Spring Boot JPA 无法删除或更新父行:外键约束失败且实体层次结构不得更改 - Cannot delete or update a parent row: a foreign key constraint fails and Entity hierarchy mustn't change
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM