I have some tables managed by Hibernate with various foreign key constraints. Cascade on delete is currently managed by Hibernate alone. For playing around with test data I often create and remove some rows by hand. It would help me a lot if I could add ON DELETE CASCADE to the foreign key constraints but I don't know if Hibernate trips over this because the database removes stuff before Hibernate does.
A lot of people seem to concentrate on DDL. My intention is not to instruct Hibernate to create DDL with SQL DELETE CASCADES. I just want to know if it does any harm if I specify an ON DELETE CASCADE in the database in addition to having JPA's cascade = CascadeType.REMOVE
on the reference annotation, e.g., @ManyToOne
.
6 Answers
Answers 1
You can use CascadeType.DELETE
, however this annotation only applies to the objects in the EntityManager
, not the database. You want to be sure that ON DELETE CASCADE
is added to the database constraint. To verify, you can configure JPA to generate a ddl file. Take a look at the ddl
file, you'll notice that ON DELETE CASCADE
is not part of the constraint. Add ON DELETE CASCADE
to actual SQL in the ddl
file, then update your database schema from the ddl. This will fix your problem .
This link shows how to use ON DELETE CASCADE
on for CONSTRAINT
in MySQL. You do this on the constraint. You can also do it in a CREATE TABLE
or ALTER TABLE
statement. It's likely that JPA creates the constraint in an ALTER TABLE
statement. Simply add ON DELETE CASCADE
to that statement.
Note that some JPA implementors do provide a means for this functionality.
Hibernate does supply this functionality using the @OnDelete
annotation, thus it is preferred to use this or simply update the ddl file if you would like to stick with standard JPA functionality.
Answers 2
You can use the native database functionality to delete the child records upon deletion of parent record.
Be aware of bi-directional relationships and to be sure, ensure you just specify insert and update in cascade (to be on safer side).
Answers 3
You mention for testing purposes. I'm guessing, execute some test, delete data, replay test...
When using second-level caching or query cache, the cache will and up being stale if you directly remove the data from the database. This might result in unexpected test results.
So yes, this will conflict with Hibernate if you use second-level / query caching as the entity's will not get evicted from cache. Make sure all caches get cleared after you directly deleted any data. See this question on how to clear cache.
The official Hibernate docs also mention this:
Be aware that caches are not aware of changes made to the persistent store by other applications. They can, however, be configured to regularly expire cached data.
Answers 4
I see two potential issues:
- If an entity that represents the table to which you cascade operations directly in the database is versioned, then it would not work because when Hibernate tries to delete records on its own, the version check would fail (Hibernate would assume concurrent thread already updated or deleted the corresponding records).
- If there are use cases in which your business logic re-persists such entity instances after removal has been cascaded to them from the parent (for example, you are deleting old parent and migrating associated children to a new one, although for better clarity I would not cascade removal at all if such a use case exists for an association, but it's up to you as it is allowed by the JPA spec), then Hibernate would just un-schedule the deletion of children and delete only the parent, so you would still end up with deleted children if you cascade deletion in the database.
Probably there are some other situations that could be problematic in some scenarios, so I would recommend not to do it.
Answers 5
Use orphanRemoval = true
clause in your @OneToMany
relation. Then when the main entity (ParameterGroup) gets deleted, every related record (Parameter) will get deleted first. Just delete ParameterGroup entity via entityManager. Also remember to set cascade
clause as CascadeType.ALL
(support all cascade operations) or CascadeType.REMOVE
(support only cascade deletion).
@Entity @Table(name = "PARAMETER_GROUP") public class ParameterGroup { @Id private Long id; @OneToMany(mappedBy = "parameterGroup", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true) private List<Parameter> parameters = new LinkedList<>(); } @Entity @Table(name = "PARAMETER") public class Parameter { @Id private Long id; @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.REFRESH) @JoinColumn(name = "PARAMETER_GROUP_ID") private ParameterGroup parameterGroup; }
From documentation:
public abstract boolean orphanRemoval (Optional)
Whether to apply the remove operation to entities that have been removed from the relationship and to cascade the remove operation to those entities.
Answers 6
Don't use cascade = CascadeType.REMOVE Documentation here
Because of your db may be destroyed. You can use formal order. Delete sub stable and then remove master table
0 comments:
Post a Comment