英文:
jpa and hibernate compare timestamp to a certain number of day
问题
我想在jpa中的某些天后删除一些行。
@Query("DELETE FROM TABLE t WHERE lastChange > :numberOfDays")
void delete(@Param("numberOfDays") int numberOfDays);
`numberOfDays`可能是10天。虽然`numberOfDays`是一个整数,`lastChange`是一个时间戳(日期时间)。
@Entity(name = "Table")
public class TableEntity
{
@Id
private String id;
@UpdateTimestamp
private Timestamp lastChange;
}
英文:
I want to delete some rows after some days in jpa.
@Repository
public interface JpaTableRepository extends JpaRepository<TableEntity, String>
{
@Query("DELETE FROM TABLE t WHERE lastChange > :numberOfDays")
void delete(@Param("numberOfDays") int numberOfDays);
}
numberOfDays
may be 10 Days. While numberOfDays
is a int, lastChange
is a TimeStamp (datetime).
@Entity(name = "Table")
public class TableEntity
{
@Id
private String id;
@UpdateTimestamp
private Timestamp lastChange;
}
答案1
得分: 1
日期操作在JPA中受到限制,但您可以使用Java方法。
服务:
int numberOfDays = 10;
LocalDateTime dateTimeMinusTenDays = LocalDateTime.now().minusDays(numberOfDays);
repository.delete(dateTimeMinusTenDays);
存储库:
@Query("DELETE FROM TABLE t WHERE lastChange > :startingLimitDateTime")
void delete(@Param("startingLimitDateTime") LocalDateTime startingLimitDateTime);
(需要将lastChange
的类型更改为java.time.LocalDateTime
)
英文:
Date operations is limited in JPA, but you could use Java methods.
Service :
int numberOfDays = 10;
LocalDateTime dateTimeMinusTenDays = LocalDateTime.now().minusDays(numberOfDays);
repository.delete(dateTimeMinusTenDays);
Repository :
@Query("DELETE FROM TABLE t WHERE lastChange > :startingLimitDateTime")
void delete(@Param("startingLimitDateTime") LocalDateTime startingLimitDateTime);
(It requires to change lastChange type to a java.time.LocalDateTime
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论