开发者社区> 问答> 正文

Hibernate怎么按时间字段查询?报错

用 Hibernate 作为持久层,数据库里news表里有一个时间字段,是datetime类型的,对应的 持久化类的属性是

Timestamp 类型的,数据库用的是 mysql,想实现一个查询操作,就是把数据库 news 表里的按照时间降序排列,并把大于某个时间的记录取出来,比如,想把比 2015-04-10 10:04:25 这个时间更晚的记录取出来,最多取出20条,我写的 HQL 代码如下:

Timestamp timeStamp = new Timestamp(time);

String queryString = "from News news order by news.time desc where news.time>?";

HibernateTemplate hibernateTemplate = this.getHibernateTemplate();

hibernateTemplate.setMaxResults(20);
<p></p>
<p>
	<span style="background-color:#DFC5A4;"> return hibernateTemplate.find(queryString, timeStamp);</span>
</p>
<p>
	但是有报错:
</p>
<p>
	</p><p>
		org.springframework.orm.hibernate3.HibernateSystemException: ordinal parameter mismatch; nested exception is org.hibernate.HibernateException: ordinal parameter mismatch<br>
		</p><p>
			at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:679) 
		</p>
		<p>
			</p><div>
				<span style="background-color:#DFC5A4;">Caused by: org.hibernate.HibernateException: ordinal parameter mismatch</span><br>
			</div>
		<p></p>
	<p></p>
<p></p>

展开
收起
爱吃鱼的程序员 2020-06-14 16:40:04 639 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    哥哥,你的sql好像是有问题吧。

    orderby是排序的,放最后的...

    from News news  where news.time>? order by news.time desc 

    2020-06-14 16:40:21
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载