I save twitter tweets to my database using JPA spring repositories. The tweet date is stored as Datetime in MySQL db. Now I want to delete all tweets that are older than one year. I saw that there is a function CURRENT_TIME, and I thought of something like CURRENT_TIME - 360. I know this is not the correct syntax, but I have no idea how to do this. Here is what I have:
@Modifying
@Transactional
@Query("DELETE FROM Tweetpost t WHERE t.createdAt > ")
int removeOlderThan();
EDIT DECISION:
Repository:
@Modifying
@Transactional
@Query("DELETE FROM Tweetpost m WHERE m.createdAt < :date")
int removeOlderThan(@Param("date") java.sql.Date date);
Services:
public void removeOldItems() {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -360);
java.sql.Date oneYear = new java.sql.Date(cal.getTimeInMillis());
tweetRepository.removeOlderThan(oneYear);
}
source
share