简体   繁体   English

Jparrepository 不删除

[英]Jparepository not deleting

Html html

            <form th:action="@{/deleteCartItem(id=${produkt.product.id})}" th:object="${produkty}" method="post">
                <div class="text-right">
                    <input type="submit" value="Delete" class="btn-sm btn-danger" />
                </div>
            </form>

Cart controller推车控制器

@GetMapping("/kosik")
public String kosik(Principal principal,Model model){
    User user = userServices.findByEmail(principal.getName());
    Cart cart = cartServices.findCartByUser(user);
    model.addAttribute("produkty",cartItemServices.findAllCartItems(cart));
    model.addAttribute("cart",cartServices.findCartByUser(user));
    model.addAttribute("user",user);
    return "cart";
}

Delete method删除方法

@PostMapping("/deleteCartItem")
public String deleteCartItem(@ModelAttribute CartItem cartItem){
    cartItemServices.deleteCartItem(cartItem.getCartItemId());
    return "redirect:/kosik";
}

Service服务

public void deleteCartItem(Integer id){
        cartItemRepository.deleteById(id);
    }

CartItem Entity购物车实体

@Entity
public class CartItem {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer cartItemId;
    @NotNull
    @Max(11)
    private int quantity;
    private double price;

    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "product_id")
    private Product product;

    @ManyToOne
    @JoinColumn(name = "cart_id")
    private Cart cart; 

Getting cartitem id works, but i cant delete cartItem.. I also tryied delete it not by id, but delete cartItem instantly..获取cartitem id 有效,但我无法删除cartItem .. 我也尝试不通过id 删除它,而是立即删除cartItem ..

Since your entity don't have id field as primary key.由于您的实体没有 id 字段作为主键。 It's not working.它不起作用。

Define the below method in CartItemRepository and use it to delete the Object.在 CartItemRepository 中定义以下方法并使用它来删除对象。

Long deleteByCartItemId(Long id);

Or rename the cartItemId field as id.或者将cartItemId 字段重命名为id。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM