Can I add 'ON DELETE CASCADE' to tables managed by Hibernate?

后端 未结 6 1708
不知归路
不知归路 2021-01-04 00:14

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

相关标签:
6条回答
  • 2021-01-04 00:45

    I see two potential issues:

    1. 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).
    2. 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.

    0 讨论(0)
  • 2021-01-04 00:46

    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 讨论(0)
  • 2021-01-04 00:47

    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).

    0 讨论(0)
  • 2021-01-04 00:56

    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.

    0 讨论(0)
  • 2021-01-04 00:57

    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.

    0 讨论(0)
  • 2021-01-04 01:04

    You can safely use ON DELETE CASCADE as long as you do not have CascadeType.REMOVE or orphanRemoval enabled to prevent propagation of the entity removal event.

    Having @OnDelete(action = OnDeleteAction.CASCADE) on your relation will force Hibernate to generate ON DELETE CASCADE for related foreign keys. However your Dialect has to indicate that such relation are supported by returning true in supportsCascadeDelete method.

    Dialect.supportsCascadeDelete()

    OnDelete

    0 讨论(0)
提交回复
热议问题