在hibernate中实现查找部分字段

今天对网站进行修改,想优化一下产品列表。原来的查找产品列表的时候,把产品的所有信息全部取出来了。我现在想只获取必须的字段。
我在网上查了一下,可以使用hql语句:“select new 包名.类名(属性1,属性2……) from 实体类来实现

我是这么写的:

    public List<TgShop> getSimpleTgShopByCid(int cid, String date,
int startIndex, int pageSize) {
// TODO Auto-generated method stub
return (List<TgShop>) this
.getHibernateTemplate()
.executeFind(
new PageHibernateCallback(
"select new "
+ className
+ "(id, shopName, begintime, endtime, successPeople, discount, maxNum, unitPrice, lowPeople,tgImage) from "
+ className
+ " t where t.stateValue=2 and t.cityInfo.id="
+ cid
+ " and '"
+ date
+ "' between t.begintime and t.endtime order by t.id desc",
startIndex, pageSize));

}

其中我的className是类名。我试着调用了一个这个方法。然后就报错了。好像是提示没有相应的构造方法。
于是我在TgShop里面添加了一个构造方法,如下:

    public TgShop(Integer id, String shopName, Date begintime, Date endtime,
Integer successPeople, Double discount, Integer maxNum,
Double unitPrice, Integer lowPeople,String tgImage) {
this.id = id;
this.shopName = shopName;
this.begintime = begintime;
this.endtime = endtime;
this.successPeople = successPeople;
this.discount = discount;
this.maxNum = maxNum;
this.unitPrice = unitPrice;
this.lowPeople = lowPeople;
this.tgImage=tgImage;
}

需要注意的是这个构造方法里面的参数是和hql语句里面的相对应的
运行一下,成功,查询了我想要的字段,以list<T>格式返回

posted on 2011-07-22 15:35  心扬  阅读(2209)  评论(1编辑  收藏  举报

导航