Hibernate – Delete the parent reference before deleting the entity (if any)

Before deleting an entity, you must delete the entity from its parent list and delete the parent entity from the entity itself.

Example (CMT in EJB) One-to-many from state to city:

public boolean delete(City city) {// Detached instance.
City managedCity = entityManager.contains(city)? city: entityManager.merge(city);
managedCity.getState().getCityList().remove(managedCity);
managedCity.setState(null);
entityManager.remove(managedCity);
return true;
}

The statement managedCity.setState(null); sets the parent of the entity to null. Since it is a managed entity, what is done to the entity Any changes will touch the underlying database. Therefore, an UPDATE statement will be generated and the state_id in the database table will be set to null.

UPDATE db.city SET state_id = ?, row_version =? WHERE ((city_id = ?) AND (row_version = ?))
bind => [null, 2, 10, 1]

This is very unpopular. Except for issuing additional In addition to the UPDATE statement, if the NOT NULL database constraint is enforced on the corresponding column in the database table, it will also try to set the state_id in the database table to null, which will cause problems.

How to delete an entity Did you delete the entity’s parent correctly before itself, or did you not even need to delete (set it to null) the parent before deleting the entity (which would cause problems in some cases. So, the answer should be yes, no)?

If you have a NOT NULL constraint, and two-way associations are best to propagate changes from the parent:

public boolean delete(City city) {// Detached instance.
City managedCity = entityManager.contains(city)? city: entityManager.merge(city) ;
managedCity.getState().getCityList().remove(managedCity);
return true;
}

And make sure to cascade the REMOVE operation from Parent to Child :

@OneToMany(cascade = CascadeType.REMOVE)

Setting the parent association to null is only valid when it has NULL FK.

< /div>

Before deleting an entity, you must delete the entity from its parent list and delete the parent entity from the entity itself.

Example (CMT in EJB) from One-to-many from state to city:

public boolean delete(City city) {// Detached instance.
City managedCity = entityManager.contains(city)? City : entityManager.merge(city);
managedCity.getState().getCityList().remove(managedCity);
managedCity.setState(null);
entityManager.remove(managedCity);< br /> return true;
}

The statement managedCity.setState(null); sets the entity’s parent to null. Since it is a managed entity, Any changes made to this entity will touch the underlying database. Therefore, an UPDATE statement will be generated and the state_id in the database table will be set to null.

UPDATE db.city SET state_id = ?, row_version =? WHERE ((city_id = ?) AND (row_version = ?))
bind => [null, 2, 10, 1]

This is very unaffected Welcome. In addition to issuing additional UPDATE statements, if the NOT NULL database constraint is enforced on the corresponding column in the database table, it will also try to set the state_id in the database table to null, which will cause problems.

< p>How to correctly delete the parent of an entity before deleting the entity itself, or it is not even necessary to delete (set it to null) the parent before deleting the entity (which can cause problems in some cases. Therefore, the answer should be yes, no )?

If you have a NOT NULL constraint, and the two-way association is best to propagate changes from the parent:

public boolean delete(City city) {// Detached instance.
City managedCity = entityManager.contains(city)? city: entityManager.merge(city);
managedCity.getState().getCityList ().remove(managedCity);
return true;
}

And make sure to cascade the REMOVE operation from Parent to Child:

@OneToMany(cascade = CascadeType.REMOVE)

Setting the parent association to null is only valid when it has NULL FK.

Leave a Comment

Your email address will not be published.