简体   繁体   English

如何按特定月份获取已createAt 参数类型Date 的所有实体?

[英]How to fetch all entities which has createdAt parameter type Date by specific Month?

I have a class that has a parameter of type Date.我有一个具有日期类型参数的 class。

class MyEntity { 
    @CreatedDate
    @Column(nullable = false, updatable = false)
    private Date createdAt;
}

How can I retrieve all those entities where the createdAt is ie: in May?如何检索 createdAt 为 ie: in May 的所有实体? Do I need to use @Query or is there a different way to perform it?我需要使用@Query 还是有不同的方法来执行它?

Do I need to use @Query?我需要使用@Query 吗?

Yes )是的 )

How can I retrieve all those entities where the createdAt is ie: in May?如何检索 createdAt 为 ie: in May 的所有实体?

If your JPA provider is Hibernate (I'm sure that it's so), you can use month() and other useful HQL functions in your queries:如果您的 JPA 提供程序是 Hibernate (我确定是这样),您可以在查询中使用month()其他有用的 HQL 函数

@Query("select e from MyEntry e where month(e.createdAt) = ?1")
List<MyEntry> getAllByMonth(int month);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring Data JPA:如何获取特定类型的所有实体以及每个实体的关联实体? - Spring Data JPA: How to fetch all entities of a specific type along with each entity's associated entities? 在特定日期范围内通过 createdAt 查询实体元素不起作用 - Query entity elements by createdAt on specific date range does not work 如何使用 Spring JPA DATA 仅获取特定的子实体及其具有特定值的父实体 - How to fetch only the specific child entity along with its Parent entities with specific value using Spring JPA DATA 将Spring ModelAttribute应用于使用特定参数类型的所有控制器 - Apply Spring ModelAttribute to all controllers that use a specific parameter type 如何使用Hibernate获取关联实体 - How to fetch associated entities with Hibernate Spring Data JPA - 如何仅使用月份和日期从Date列中获取数据? - Spring Data JPA - How can I fetch data from a Date column using only month and day? springmvc如何获取参数? - springmvc how to fetch the parameter? 是否可以从父抽象实体中获取所有子实体? - Is it possible to fetch all child entities from a parent abstract entity? HQL 查询以选择按与已建立实体相关的参数值分组的所有唯一实体 - HQL query to select all unique entities grouped by the parameter value that is in relation to founded entities 动态获取关联实体 - Dynamically fetch associated entities
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM