Select MySql 数据库中基于月份的所有记录

Select all records from MySql database based on month

我想使用 Hibernate 条件 API 从 MySQL 日期字段中获取所有记录。基本上我希望查询 select 从日期到预定义月份(例如一月)的所有行。这是我的 MySql 成功执行的查询

select * from user u where month(u.birth_date) = 1; //1 for January

我想将此 MySql 查询转换为休眠条件,例如

criteria.add(Restrictions.eq("month(birth_date)", month));

但是报错

birth_date 的数据库数据类型是时间戳。

提前致谢。

你得自己写表情。然后将该表达式与条件一起使用。你可以看到这个 link 关于如何创建表达式。 How to query on a month for a date with Hibernate criteria