@Override @Transactional(readOnly = false) public int deleteActionsByStatusAndLastModifiedBefore(final Set<Status> status, final long lastModified) { if (status.isEmpty()) { return 0; } /* * We use a native query here because Spring JPA does not support to * specify a LIMIT clause on a DELETE statement. However, for this * specific use case (action cleanup), we must specify a row limit to * reduce the overall load on the database. */ final int statusCount = status.size(); final Status[] statusArr = status.toArray(new Status[statusCount]); final String queryStr = String.format(getQueryForDeleteActionsByStatusAndLastModifiedBeforeString(database), formatInClauseWithNumberKeys(statusCount)); final Query deleteQuery = entityManager.createNativeQuery(queryStr); IntStream.range(0, statusCount) .forEach(i -> deleteQuery.setParameter(String.valueOf(i), statusArr[i].ordinal())); deleteQuery.setParameter("tenant", tenantAware.getCurrentTenant().toUpperCase()); deleteQuery.setParameter("last_modified_at", lastModified); LOG.debug("Action cleanup: Executing the following (native) query: {}", deleteQuery); return deleteQuery.executeUpdate(); }
@Override @Transactional(readOnly = false) public int deleteActionsByStatusAndLastModifiedBefore(final Set<Status> status, final long lastModified) { if (status.isEmpty()) { return 0; } /* * We use a native query here because Spring JPA does not support to * specify a LIMIT clause on a DELETE statement. However, for this * specific use case (action cleanup), we must specify a row limit to * reduce the overall load on the database. */ final int statusCount = status.size(); final Status[] statusArr = status.toArray(new Status[statusCount]); final String queryStr = String.format(getQueryForDeleteActionsByStatusAndLastModifiedBeforeString(database), formatInClauseWithNumberKeys(statusCount)); final Query deleteQuery = entityManager.createNativeQuery(queryStr); IntStream.range(0, statusCount) .forEach(i -> deleteQuery.setParameter(String.valueOf(i), statusArr[i].ordinal())); deleteQuery.setParameter("tenant", tenantAware.getCurrentTenant().toUpperCase()); deleteQuery.setParameter("last_modified_at", lastModified); LOG.debug("Action cleanup: Executing the following (native) query: {}", deleteQuery); return deleteQuery.executeUpdate(); }