0

我需要使用 Hibernate 从特定月份和年份中提取记录。

我的代码:

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

如何解决此错误?

4

2 回答 2

0

There are two thing I want to mention:

first: year and month variables should cross

restrictionAnno = Restrictions.eq("year(dtControllo)",year);
restrictionMese = Restrictions.eq("month(dtControllo)",month);

second: you could try "lt" and "gt" methods for catching records in between.

restrictionAnno = Restrictions.lt(field, monthEndDate);
restrictionMese = Restrictions.gt(field, monthStartDate);
于 2013-02-20T16:04:09.367 回答
0

我过去处理这个问题的一种方法是通过在我的数据库中创建一个视图来将休眠完全排除在外。因此,如果我有一个包含 、 、 列的表,Event我将使用我的 RDBMS 创建一个视图,该视图从. EVENT_IDSTART_DATENAMESTART_DATE

该视图看起来像:

V_EVENT

EVENT_ID、START_DATE、NAME、START_MONTH、START_YEAR

拥有此视图后,您可以创建实体并运行使用视图中的两个附加列START_YEARSTART_MONTH. 这简化了在 Hibernate/ORM 中创建 where 子句语句。

于 2012-12-04T10:32:22.573 回答