I have two object. Let it be company and employee.
CREATE TABLE company (
company_id BIGINT(20) NOT NULL AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
chief_id BIGINT(20) NOT NULL,
PRIMARY KEY (company_id),
CONSTRAINT fk_company_chief
FOREIGN KEY (chief_id)
REFERENCES employee (employee_id)
ON DELETE NO ACTION
ON UPDATE NO ACTION);
CREATE TABLE employee(
employee_id BIGINT(20) NOT NULL AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
company_id BIGINT(20) NOT NULL,
PRIMARY KEY (employee_id),
CONSTRAINT fk_employee_company
FOREIGN KEY (chief_id)
REFERENCES employee (company_id)
ON DELETE NO ACTION
ON UPDATE NO ACTION);
And my classes look like:
class Employee {
long id;
String name;
Company company;
}
class Company{
long id;
String name;
Employee chief;
}
Then I want to delete company with all its employees. I do it in a single transaction. I am getting smth like "java.sql.BatchUpdateException: Column 'chief_id' cannot be null"
I am able to delete only after making one of the columns nullable. For example "chief_id BIGINT(20) NULL," and then making company.chief=null before delete.
In the project we don't use Hibernate cascades and I am not able to change Database cascades.
We are using MySql 5.0.
I need smth like: disable constraints->remove entity->enable constraints. The disabled state should be accessible only within current transaction. I thought it was default behavior.