简体   繁体   中英

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 MyEntity { 
    @CreatedDate
    @Column(nullable = false, updatable = false)
    private Date createdAt;
}

How can I retrieve all those entities where the createdAt is ie: in May? Do I need to use @Query or is there a different way to perform it?

Do I need to use @Query?

Yes )

How can I retrieve all those entities where the createdAt is 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM