Delete records from more than 1 year ago

前端 未结 4 658
说谎
说谎 2021-01-18 02:26

I\'m saving twitter tweets in my database with spring JPA Repositories. The date of the tweet is saved as Datetime in the MySQL db. Now I want to delete all tweets that are

4条回答
  •  感情败类
    2021-01-18 02:55

    SOLVED:

    Repository:

    @Modifying
        @Transactional
        @Query("DELETE FROM Tweetpost m WHERE m.createdAt < :date")
        int removeOlderThan(@Param("date") java.sql.Date date);
    

    Service:

    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);
    
    
        }
    

提交回复
热议问题