Reputation: 12876
I'm observing a kind of strange behavior in my Spring application. Unfortunately I cannot share the complete code, but basically this is what it looks like:
// the repository
@Repository
public interface InboxRepo extends JpaRepository<Inbox, Long> {}
// the service
@Transactional
public void deleteInbox(long id) {
inboxRepo.deleteById(id);
}
When calling deleteInbox()
, there is no exception or any kind of error but the Inbox
item is not deleted from the database. Setting spring.jpa.show-sql=true
shows that there isn't even a DELETE
statement, i.e. for whatever reason, the code doesn't actually issue the deletion.
When defining a derived delete method in the repository, then the deletion works, but it doesn't yet make sense to me:
@Repository
public interface InboxRepo extends JpaRepository<Inbox, Long> {
// this seems to work
@Modifying
@Query("delete from Inbox i where i.id = ?1")
void delete(long id);
}
Dleting directly via an EntityManager
also works. But what could be the reason that the "standard" JpaRepository
methods don't work here?
Upvotes: 3
Views: 2821
Reputation: 12876
I found the root cause. There was another entity having a reference to Inbox
like this:
@OneToMany(mappedBy = "inbox", cascade = ALL, fetch = FetchType.EAGER)
private Set<Inbox> inbox = new HashSet<>();
The FetchType.EAGER
in combination with the cascade caused the problem, i.e. as soon as the Inbox
was deleted, this reference caused the Inbox
to get "re-persisted". Setting FetchType.LAZY
resolved the problem.
Upvotes: 5