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, eg, @ManyToOne
.
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.
I see two potential issues:
Probably there are some other situations that could be problematic in some scenarios, so I would recommend not to do it.
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).
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.
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.
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.
From my experiments, since I have struggled with this for a couple hours, using updatable = false
in the @JoinColumn will prevent Hibernate from doing
update theTable set theReference = null where theIdentifier = ?
For example (in Kotlin):
@Entity
@Table(name = "course")
class Course(
@Column(name = "title")
var title: String? = null,
@OneToMany(cascade = [CascadeType.PERSIST])
@JoinColumn(name = "course_id", updatable = false)
val reviews: MutableList<Review> = mutableListOf()
) {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
var id: Int? = null
}
A few days back I was looking for solutions here about deleting child rows on deleting parent row(Casacading Deletes), I seem to have solved it on a project am currently working on. I use Casade.ALL for my bidirectional many-to-one relationship. Solution:
// Parent Entity.
@Entity
public class Parent implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "parent")
private List<RequisitionDetail> children = new LinkedList<>();
...
// Getters & Setters.
...
}
// Child Entity.
@Entity
public class Child implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "parent_id")
private Parent parent;
...
// Getters & Setters.
...
}
// Save your entities as below.
public void requisitionInsert() {
try {
Parent parent = new Parent();
Child child = new Child();
List<Child> children = new LinkedList<>();
...
// Add parent to child
child.setParent(parent);
// Add child to list
children.add(child);
// Add children list to parent
parent.setChilds(children);
// Save parent entity
ParentDao.saveParent(parent);
} catch (Exception e) {
throw new RuntimeException();
}
}
My cascading deletes failed at first since my parent rows had no child rows and doing it this way sloved my errors and cascading deletes work fine too.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.