I'm working on some legacy code so unfortunately I'm unable to answer why the code has been designed as is. There are two entities: Inbox
and Upload
.
public class Upload {
// other properties...
@ManyToOne(cascade = PERSIST)
@JoinColumn(name = "inbox_id", nullable = false)
private Inbox inbox;
}
public class Inbox {
// other properties...
}
Inbox
does not have any relationship to Upload
. Only Upload
references the Inbox
via the inbox_id
.
Now I'd ilke to delete an Upload
item like this:
public void deleteUpload(long uploadId) {
final Upload upload = uploadRepo.findById(uploadId).orElseThrow(EntityNotFoundException::new);
uploadRepo.delete(upload);
}
This works fine. However, I'd also like to delete the corresponding Inbox
but I have no idea how to do that. My current approach looks like this, but this does not delete in Inbox
item.
public void deleteUpload(long uploadId) {
final Upload upload = uploadRepo.findById(uploadId).orElseThrow(EntityNotFoundException::new);
uploadRepo.delete(upload);
inboxRepo.deleteById(upload.getInbox().getId());
}
Update 1
I tried @ManyToOne(cascade = {PERSIST, REMOVE})
but this leaves the Inbox
remain in the database.
What puzzles me even more: Setting spring.jpa.show-sql=true
doesn't show any delete
statement for for the Inbox
entity.
Update 2
In this similar SO question I found that the following actually works:
@Modifying
@Query("delete from Innox i where i.id = ?1")
void delete(long id);
Although I feel that this is not "the" way to do it.