In my Spring Boot app, I have a table message
which has a child table message_recipient
i.e. the latter holds a foreign key to the former. These are both mapped to domain classes using JPA.
I’ve defined the following Spring Data JPA repository
public interface MessageRepository extends JpaRepository<Message, UUID> {
int deleteBySavedByBusinessUnitAndIdAndStatus(
BusinessUnit businessUnit, UUID messageId, MessageStatus status);
}
If I call this method, the row in message
and the associated child rows in message_recipient
are deleted. However, if I define the JPQL statement myself
public interface MessageRepository extends JpaRepository<Message, UUID> {
@Modifying
@Query("""
delete from Message m
where m.savedByBusinessUnit = :businessUnit
and m.id = :messageId
and m.status = :status""")
int deleteBySavedByBusinessUnitAndIdAndStatus(
BusinessUnit businessUnit, UUID messageId, MessageStatus status);
}
The deletion of the row in message
fails because there are rows referencing it in message_recipient
.
Given does the deletion cascade to child tables in the first case, but not in the second? Is there a way to force the deletion to cascade in the second case (without changing the table definitions)?