简体   繁体   English

从表中删除行抛出 ConstraintViolationException

[英]Remove row from table throws ConstraintViolationException

Im having a problem when i want to delete the product from the database, deleting it, it should be removed from all the orders that contain that product.当我想从数据库中删除产品时遇到问题,删除它,它应该从包含该产品的所有订单中删除。 But when i try to do it this is the error i get:但是当我尝试这样做时,这是我得到的错误:

"error_message": "Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [fkbjvki7e3gm7vrphs73g4x7d2g]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement"

This is my Order class:这是我的订单 class:

@Entity
@Table(name="orders")
public class Order{
    private @Id
    @GeneratedValue
    Long id;
    @OneToMany(mappedBy = "order", cascade = CascadeType.ALL,orphanRemoval = true)
    private List<ProductOrderDetails> orderedProducts = new ArrayList<>();
public void addProduct(Product product, int quantity) {
        ProductOrderDetails orderedProduct = new ProductOrderDetails(this,product,quantity);
        orderedProducts.add(orderedProduct);
        product.getProductOrderDetails().add(orderedProduct);
        totalOrderPrice+=product.getPrice()*quantity;
    }

    public void removeProduct(Product product,int quantity) {
        ProductOrderDetails orderedProduct = new ProductOrderDetails( this, product,0);
        product.getProductOrderDetails().remove(orderedProduct);
        orderedProducts.remove(orderedProduct);
        orderedProduct.setOrder(null);
        orderedProduct.setProduct(null);
        totalOrderPrice-=product.getPrice()*quantity;
    }
}

This is my Product class这是我的产品 class

@Entity
@Table
public class Product {
    private @Id
    @GeneratedValue
    Long id;
    private String name;
    @OneToMany(mappedBy = "order", cascade = CascadeType.MERGE,orphanRemoval = true)
    private List<ProductOrderDetails> productOrderDetails = new ArrayList<>();
}

ProductOrderID产品订单ID

@Embeddable
public class ProdOrderId implements Serializable {
    @Column(name = "order_id")
    private Long orderId;

    @Column(name = "product_id")
    private Long productId;
}

Many to many column of Products and Orders多对多列的产品和订单

@Entity
@Table
public class ProductOrderDetails implements Serializable{

    @EmbeddedId
    @JsonIgnore
    private ProdOrderId id;


    @ManyToOne
    @MapsId("orderId")
    @JsonIgnore
    Order order;

    @ManyToOne
    @MapsId("productId")
    Product product;

    private int quantity;
}

This is my controller method这是我的 controller 方法

@DeleteMapping("/{id}")
    ResponseEntity<?> deleteProduct(@PathVariable Long id)
    {
        repository.deleteById(id);
        return ResponseEntity.noContent().build();
    }

I don't think this is doing what you think it's doing:我不认为这是在做你认为它在做的事情:

ProductOrderDetails orderedProduct = new ProductOrderDetails( this, product,0);
product.getProductOrderDetails().remove(orderedProduct);

If you debug your code or check the return value of remove you will find that it is returning false , which means nothing was removed.如果您调试代码或检查remove的返回值,您会发现它返回false ,这意味着没有任何内容被删除。

You're just creating a new ProductOrderDetails and then trying to remove it from product.getProductOrderDetails() , but it doesn't exist in it.您只是在创建一个新的ProductOrderDetails ,然后尝试将其从product.getProductOrderDetails()中删除,但其中不存在。 You need to find the right element to remove from that collection.您需要找到要从该集合中删除的正确元素。

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

相关问题 Springboot 2 CrudRepository.save总是抛出ConstraintViolationException - Springboot 2 CrudRepository.save always throws ConstraintViolationException 在使用Spring JPA在两个实体之间进行多对多映射的情况下,在表中保存带有嵌入式键的行的同时获取ConstraintViolationException - Getting ConstraintViolationException while saving a row with embedded key in the table with many-to-many mapping between two entities using Spring JPA 如何删除联接表中的行? - How to remove a row in a joined table? 如何从ConstraintViolationException获取查询参数名称 - How to get query parameter name from ConstraintViolationException 将计数添加到从表中提取的每一行 - Add count to each row extracted from table 从表Thymeleaf Springboot中删除一行 - Deleting a row from a table Thymeleaf Springboot ValidationException与ConstraintViolationException进行验证 - ValidationException vs ConstraintViolationException for validation Hibernate - Spring - ConstraintViolationException - UniqueConstraint - Hibernate - Spring - ConstraintViolationException - UniqueConstraint 自定义 - ConstraintViolationException - SQL 异常 - Custom - ConstraintViolationException - SQL Exception Spring 引导 PersistentBag remove() 抛出 UnsupportedOperationException - Spring Boot PersistentBag remove() throws UnsupportedOperationException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM