如何在Hibernate 3.0中实现mysql date_sub()函数 [英] How to implement mysql date_sub() function in Hibernate 3.0

查看:187
本文介绍了如何在Hibernate 3.0中实现mysql date_sub()函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我必须在hibernate中写一个查询,比如

Hi i have to write a query in hibernate like

SELECT * FROM `tablename` where created_at> DATE_SUB(curdate(),INTERVAL 7 DAY) 

在hibernate中。我已发布有关此主题的查询。但没有人帮助我。在hiberanate中可以写这样的查询吗?请回复。

in hibernate. I have already post a query regarding this topic. But no one helps me. Is it possible in hiberanate to write query like this? Please reply.

推荐答案

您可以使用原生SQL查询

You can either use a native SQL query

String sql = "SELECT * FROM tablename WHERE created_at > DATE_SUB(curdate(), INTERVAL 7 DAY)"
Query query = session.createSQLQuery(sql);
List result = query.list();

或者您可以使用Hibernate Criteria Restrictions.sqlRestriction

Or you can use Hibernate Criteria Restrictions.sqlRestriction

String sqlWhere = "{alias}.created_at > DATE_SUB(curdate(), INTERVAL 7 DAY)";
Criteria criteria = session.createCriteria(MyEntity.class);
criteria.add(Restrictions.sqlRestriction(sqlWhere));
List result = criteria.list();

希望有所帮助。

这篇关于如何在Hibernate 3.0中实现mysql date_sub()函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆