我是HQL的新手,我需要进行以下MSSQLServer查询,该查询可在所有数据库中执行每月和每年的销售额,
select count(sales) as monthsales from salesdairy where propertytype in ('item1') and MONTH(time) = MONTH(getdate())
select count(sales) as yearsales from salesdairy where propertytype in ('item1') and YEAR(time) = YEAR(getdate())
我对功能感到震惊,MONTH(time) = MONTH(getdate()),YEAR(time) = YEAR(getdate())
HQL中是否有任何等效功能。如果有人可以给我HQL同等的水平,我将不胜感激。
在HQL中,您有实体(类对象)而不是表。我想您的表salesdiary成为SalesDiary实体。
第一个查询:
select count(S.id)
from SalesDiary S
where S.propertytype in ('item1')
and MONTH(S.time) = MONTH(current_date)
第二个查询:
select count(S.id)
from SalesDiary S
where S.propertytype in ('item1')
and YEAR(S.time) = YEAR(current_date())
函数MONTH获取输入日期的月份数,年份函数获取输入日期的年份数。
current_date()等效于SQL Server GETDATE()。
注释后编辑很奇怪,Hibernate必须使用下划线DBMS的正确功能进行转换。
如果可以使用参数,请尝试以下操作:
select count(S.id)
from SalesDiary S
where S.propertytype in ('item1')
and YEAR(S.time) = YEAR(:currDate)
当您将HQL查询转换为对象Query时,您将以以下方式用setDate函数替换currDate参数:
String hql =
"select count(S.id)
from SalesDiary S
where S.propertytype in ('item1')
and YEAR(S.time) = YEAR(:currDate)";
Query q = session.createQuery(hql);
q.setDate("currDate", new GregorianCalendar().getTime());
q.list();
我希望现在可以了;)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句