spring+hibernate中的Result object returned from HibernateCallback isn't a List

本文解决了在Spring框架中使用HibernateCallback时,由于executeFind方法返回List类型导致无法使用uniqueResult的问题。通过将executeFind替换为execute方法实现返回非List类型的单一结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Ok the problem is that for executeFind() the return type is List....so there is no way to use uniqueResult() within the callback from executeFind()...may be we should use execute()

上面这段话来自http://forum.springframework.org/showthread.php?t=58370

在使用executeFind方法时,如果返回的不是List类型的数据则会出现引异常

例:
public Integer findCount(final Class clazz,final String str) {
return Integer.parseInt(getHibernateTemplate().executeFind(new HibernateCallback()
{

@Override
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
StringBuilder sb=new StringBuilder();
sb.append("select count(*) from ");
sb.append(clazz.getName());
sb.append(" ");
sb.append(str);
log.info(sb.toString());
return session.createQuery(sb.toString()).uniqueResult();
}

}).toString());
}

解决方法是把executeFind方法改为execute方法

public Integer findCount(final Class clazz,final String str) {
return Integer.parseInt(getHibernateTemplate().execute(new HibernateCallback()
{

@Override
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
StringBuilder sb=new StringBuilder();
sb.append("select count(*) from ");
sb.append(clazz.getName());
sb.append(" ");
sb.append(str);
log.info(sb.toString());
return session.createQuery(sb.toString()).uniqueResult();
}

}).toString());
}

有问题?直接google! <script type="text/javascript"><!-- google_ad_client = "pub-0757178726455256"; google_ad_format = "js_sdo"; google_cts_mode ="rs"; google_num_cts = "2"; google_searchbox_width = 215; google_searchbox_height = 26; google_link_target = 2; google_logo_pos = "left"; google_rs_pos = "right"; google_ad_height = 35; google_ad_width = 760; // --></script><script src="http://pagead2.googlesyndication.com/pagead/show_sdo.js" type="text/javascript"></script>

<script type="text/javascript"><!-- google_ad_client = "pub-0757178726455256"; /* 728x90, 创建于 09-6-17 */ google_ad_slot = "1633515232"; google_ad_width = 728; google_ad_height = 90; // --></script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值