0

When trying to delete an element from the table that is the element to which other elements of the same table are linked, an error occurs: java.sql.SQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (company.product, CONSTRAINT FKq39fk2qbnqmgxuiqh5wrccgy8 FOREIGN KEY (product) REFERENCES product (id))

My entity includes an object of the same class. Here is her code:

@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class Product {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column (name = "name")
    private String name;

    @Column (name = "cipher")
    private String cipher;

    @Column (name = "route")
    private String route;

    @Enumerated (value = EnumType.STRING)
    @Column (name = "type")
    private Type type;

    @JoinColumn (name = "product")
    @ManyToOne
    private Product mainProduct;

    public enum Type{
        DETAIL ("Деталь"),
        ASSEMBLY ("Сборочная единица"),
        PURCHASED ("Покупная"),
        MATERIAL ("Материал"),
        NORMALIZED ("Стандарное изделие"),
        ASSEMBLY_NORMAL ("Сборочная нормаль");

        private String type;

        Type(String type){
            this.type = type;
        }

        public String getType(){
            return type;
        }
    }
}

Why can't I delete the parent element by deleting the child elements along with it?

Dinislam
  • 31
  • 3
  • Almost, but when I use that method I get another mistakes: "java.lang.StackOverflowError: null" and "java.sql.SQLNonTransientConnectionException: Communications link failure during rollback(). Transaction resolution unknown." – Dinislam Aug 30 '22 at 08:08
  • That's it, I fixed the problem!:) The additional errors mentioned in my previous comment were eliminated by changing Set to ArrayList:) Thank you so much!!! – Dinislam Aug 30 '22 at 08:13

0 Answers0