Hibernate标准获得2012年的记录 [英] Hibernate criteria for get records which have year 2012
问题描述
我需要从搜索条件中输入年份的所有记录。
例如:
字符串年=2012;
在hibernate中,它适用于
criteria.add(Restrictions.like(createDate,%+ year +%));
createDate格式为DD-MM-YY。
请建议..
像
只适用于字符串。对于日期字段,您可以使用 Restrictions.between()
在'01 / 01/2012 00:00:00'和'12'之间生成 / 31/2012 23:59:59'
for where子句
SimpleDateFormat df = new SimpleDateFormat (dd-MM-yyyy HH:mm:ss);
Date fromDate = df.parse(1-1-2012 00:00:00);
日期toDate = df.parse(2012-03-12 23:59:59);
criteria.add(Restrictions.between(createDate,fromDate,toDate));
另外,请注意Criteria API中使用的所有属性都是Java属性名称,但不是实际的列名称。
i need all records which have year entered from search criteria. for ex: String year = "2012";
In hibernate it work for
criteria.add(Restrictions.like("createDate", "%"+year+"%"));
createDate is in format DD-MM-YY.
Please suggest..
like
only applies for the String . For the date field , you can use Restrictions.between()
to generate between '01/01/2012 00:00:00' and '12/31/2012 23:59:59'
for the where clause
SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
Date fromDate = df.parse("1-1-2012 00:00:00");
Date toDate = df.parse("31-12-2012 23:59:59");
criteria.add(Restrictions.between("createDate", fromDate, toDate));
Also , please note that all the properties used in the Criteria API is the Java property name , but not the actual column name.
这篇关于Hibernate标准获得2012年的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!