I am having a hard time trying to catch the exception raised by a foreign key Constraint error:
Example:
DAO dao = new DAO();
try {
DAO.begin(); //starts the transaction
DAO.delete(object); //delete the object
DAO.commit();
} catch (Exception) { //this hibernate exception is ignored
DAO.rollback();
Sytem.out.print("--catched---");
}
Hibernate: delete from Logradouro where id=?
fev 13, 2019 2:32:22 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 1451, SQLState: 23000
fev 13, 2019 2:32:22 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: Cannot delete or update a parent row: **a foreign key constraint fails** (`hibernateDB`.`endereco`, CONSTRAINT `Fk_EndLogr` FOREIGN KEY (`logradouro_id`) REFERENCES `logradouro` (`id`))
fev 13, 2019 2:32:22 PM org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl release
INFO: HHH000010: On release of batch it still contained JDBC statements
fev 13, 2019 2:32:22 PM org.hibernate.internal.SessionImpl$5 mapManagedFlushFailure
ERROR: HHH000346: Error during managed flush [could not execute statement]