Prevent the delete of a parent object if it has JPA child objects

Asked

Viewed 32 times

0

Is there any way defined by JPA that prevents the exclusion of a parent object if it has a child object attached to it? For example:

I have the class (Entity) Department, in which I have the following field:

@OneToMany(mappedBy = "departamento" )
public List<Usuario> getEmpregados() {
    return empregados;
}

The field employees is a list of employees linked to this Department.

I need that when requesting the exclusion of a department, this action does not occur if you have any employee linked to it.

Analyzing the JPA Cascade Type, I did not find any option that makes this lock, the solution I see is to do the validation manually before executing the Jparepository delete command for the Department.

Is there any way that JPA can enforce the integrity of the bank and prevent a parent object from being excluded by having children attached, something like an annotation?

1 answer

0


I transferred to the bank the responsibility to manage the referential integrity. I changed in applicatoin.propertis the dialect used for JPA chat with Mysql as below:

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.Mysql8dialect, so the tables were generated above the Innodb engine thus creating the relationships in the database.

Before it was version 5, the tables were generated using the Myisam engine.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.