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?