gpt4 book ai didi

java - JPA查询: how to use select * from my_table where created_at > now() - INTERVAL '3 months' ?

转载 作者:行者123 更新时间:2023-12-01 19:36:33 26 4
gpt4 key购买 nike

如何添加条件为 created_at > now() - INTERVAL '3month'where 子句?

JPA 实体列:

@Column(name = "created_at")
@CreationTimestamp
@Temporal(TemporalType.TIMESTAMP)

最佳答案

您可以使用命名查询。

用以下方式注释您的实体类:

@NamedQuery(name = "Entity.findByCreatedDateGreaterThan", query = "FROM Entity e WHERE u.createdDate > :date")

要使用此命名查询:

public List<Entity> getEntityByCreatedDateGreaterThan() {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_MONTH, -90);
Query namedQuery = getEntityManager().createNamedQuery("Entity.findByCreatedDateGreaterThan");
namedQuery.setParameter("date", calendar.getTime());
return namedQuery.getResultList();
}

关于java - JPA查询: how to use select * from my_table where created_at > now() - INTERVAL '3 months' ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59213643/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com