I'm working on a java web project that uses:
Spring
4.3.2Hibernate
5.2.2/JPA2.0+MySQL5InnoDBDialectMySQL
5.6.15-innoDB(on EasyPHP/PHPMyAdmin) + JDBCconnector 6.0.4
I'm facing a error when i want to delete some data. But, this problem does exist only if i want to Delete a parent row that have children and one of his children also have at least one child.
Goal: I want the action/query to delete the selected row and its children and grandchildren.
Error code: in server output
28-Sep-2016 11:51:30.345 ERROR [http-nio-80-exec-42] org.hibernate.internal.ExceptionMapperStandardImpl.mapManagedFlushFailure HHH000346: Error during managed flush [org.hibernate.exception.ConstraintViolationException: could not execute statement] org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
Parent classe: Pavillon
//...
private Set<Table> pavTables;
//...
@JsonIgnore
@Cascade(CascadeType.ALL)
@OneToMany(orphanRemoval = true, mappedBy = "pavillon", fetch = FetchType.EAGER)
public Set<com.optimal.waiter.component.model.Table> getPavTables() {
return pavTables;
}
//...setters & others
Child classe: Table
//...
private Pavillon pavillon;
//...
@NotNull
@Basic(optional = false)
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "PAV_ID")
public Pavillon getPavillon() {
return pavillon;
}
@JsonIgnore
@Cascade(CascadeType.ALL)
@OneToMany(orphanRemoval = true, mappedBy = "table", fetch = FetchType.EAGER)
public Set<Reservation> getTableReservations() {
return tableReservations;
}
//...setters & others
Grandchild class: Reservation
//...
private Table table;
//...
@NotNull
@Basic(optional = false)
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "TAB_ID", nullable = false)
public Table getTable() {
return table;
}
//...setters & others
SQL:
CREATE TABLE PAVILLONS
(
PAV_ID INT(10) NOT NULL AUTO_INCREMENT,
PAV_NOM VARCHAR(25) NOT NULL,
PAV_DES TEXT,
PAV_TYPE INT(1) NOT NULL DEFAULT 0,
PRIMARY KEY (PAV_ID)
);
CREATE TABLE TABLES
(
TAB_ID INT(10) NOT NULL AUTO_INCREMENT,
PAV_ID INT(10) NOT NULL DEFAULT 1,
TAB_DISPO TINYINT(1) NOT NULL DEFAULT 1,
TAB_TYPE INT(1) NOT NULL DEFAULT 0,
PRIMARY KEY (TAB_ID)
);
CREATE TABLE RESERVATIONS
(
RES_ID INT(10) NOT NULL AUTO_INCREMENT,
TAB_ID INT(10) NOT NULL,
PRIMARY KEY (RES_ID)
);
ALTER TABLE TABLES ADD CONSTRAINT FK_PAV_TAB FOREIGN KEY (PAV_ID)
REFERENCES PAVILLONS (PAV_ID) ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE RESERVATIONS ADD CONSTRAINT FK_TABLE_RESERVATION FOREIGN KEY (TAB_ID)
REFERENCES TABLES (TAB_ID) ON DELETE CASCADE ON UPDATE CASCADE;
PS: the error still even when i try to delete then using SQL query directly in PHPMyAdmin.
Error code: in server output
ERROR 1451: 1451: Cannot delete or update a parent row: a foreign key constraint fails (
commandes, CONSTRAINTFK_TAB_COMMFOREIGN KEY (TAB_ID) REFERENCEStables(TAB_ID)) SQL Statement: DELETE FROMpavillonsWHEREPAV_ID='2'
That's why i think the problem is in DB side.
Waiting for your help, any suggestion will help.
show create table myTablewhere myTable are at least the ones for the childrenCascadeannotation and use@OneToManyas@OneToMany(..., cascade=CascadeType.ALL)- and do note thatCascadeTypehere isjavax.persistence.CascadeType!javax.persistence.CascadeTypethere was more unwanted things to set in theDAO. No need for this and it is not the problem in this case. Thanks for your help.Commandeattached with the class Table using@OneToOnerelation. it mean conceptually that a Table can have many reservations and a command is setter to one table and the table ID is required on it. I think this is the problem thanks.