Hibernate 获取特定月份和年份的记录
我需要使用Hibernate从特定月份和年份提取记录 我的代码:Hibernate 获取特定月份和年份的记录,hibernate,date,hql,Hibernate,Date,Hql,我需要使用Hibernate从特定月份和年份提取记录 我的代码: if (!Converter.isStringNullOrEmpty(month ) && !Converter.isStringNullOrEmpty(year) ) { restrictionAnno = Restrictions.eq("year(dtControllo)",month ); restrictionMese = Restrictions.eq("month(dtControll
if (!Converter.isStringNullOrEmpty(month ) && !Converter.isStringNullOrEmpty(year) ) {
restrictionAnno = Restrictions.eq("year(dtControllo)",month );
restrictionMese = Restrictions.eq("month(dtControllo)",year);
}
这将产生一个休眠异常:
could not resolve property: month(dtControllo) of: it.iet.ortles.hbean.TableauPresenza
如何解决此错误?我过去处理此问题的一种方法是通过在数据库中创建视图,将hibernate从图片中删除。因此,如果我有一个表
Event
,其中列Event\u ID
,START\u DATE
,NAME
,我会使用我的RDBMS创建一个视图,从START\u DATE
提取月份和年份
视图将类似于:
V_事件
事件ID、开始日期、名称、开始月份、开始年份
拥有此视图后,您可以创建一个实体并运行查询,该查询使用视图中的两个附加列
START\u YEAR
和START\u MONTH
。这简化了在Hibernate/ORMs中创建where子句语句的过程。我想提到两件事:
第一:年和月变量应该交叉
restrictionAnno = Restrictions.eq("year(dtControllo)",year);
restrictionMese = Restrictions.eq("month(dtControllo)",month);
第二:您可以尝试使用“lt”和“gt”方法来捕获介于两者之间的记录
restrictionAnno = Restrictions.lt(field, monthEndDate);
restrictionMese = Restrictions.gt(field, monthStartDate);
我试着这样做:DetachedCriteria=DetachedCriteria.forClass(TableauPresenza.class);标准。添加(限制。le(“dtControllo”,toStart(年、月));标准。添加(Restrictions.ge(“dtControllo”,toEnd(filtroanno,filtromese));