Skip to content

Instantly share code, notes, and snippets.

@revseev
Last active April 5, 2020 16:41
Show Gist options
  • Select an option

  • Save revseev/ecde4739d8b8642169b779b6eb8fdec3 to your computer and use it in GitHub Desktop.

Select an option

Save revseev/ecde4739d8b8642169b779b6eb8fdec3 to your computer and use it in GitHub Desktop.
JPQL delete with JOIN query
@Override
@Transactional(propagation= Propagation.MANDATORY)
public void bulkDeleteByUserId(Long id) {
// Write all pending changes to the DB
entityManager.flush();
// Remove all entities from the persistence context
entityManager.clear();
entityManager.createQuery("DELETE FROM StudentReviewAnswer x " +
"WHERE x IN (SELECT sra FROM StudentReviewAnswer sra " +
"JOIN StudentReview sr ON sra.studentReview.id = sr.id " +
"WHERE sr.user.id =:id)")
.setParameter("id", id)
.executeUpdate();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment