通过顺序占位符?来填充参数:
1)hibernate 2 中通过session.find方法来填充
| session.find("from TUser user where user.name=?", "Erica", Hibernate.STRING); |
多个参数的情况:
| Object[] args = new Object[] {"Erica", new Integer(20)}; |
2)通过Query接口进行参数填充:
| Query query = session.createQuery("from TUser user where user.name=? and user.age>?"); |
通过引用占位符来填充参数:
| String hql = "from TUser where name=:name";
|
如何把两个时间类型的参数传递到find的第二个参数
public List findAll(Date begin, Date end) throws Exception {
String hql="from Salebill where saleDate bewteen ? and ?";
Object[] value={begin,end};
List list = this.getHibernateTemplate().find(hql, value);
return list;
}
List moreCats = this.getHibernateTemplate().find(
"from Cat as cat where " + "cat.name = 'Fritz' or cat.id = ? or cat.id = ?",
new Object[] { id1, id2 },
new Type[] { Hibernate.LONG, Hibernate.LONG }
);
public AuctionUser findUserByItemAndPrice(Integer itemId , Double price)
{
Object[] args = {itemId , price} ;
List l = getHibernateTemplate().find("from Bid as bid where bid.bidItem.id = ? and bid.bidPrice = ?",args);
if (l.size() >= 1)
{
Bid b = (Bid)l.get(0);
return b.getBidUser();
}
else
{
return null;
}
}
以上资料来源:http://hi.baidu.com/solidbullet/blog/item/1e1027462ada912f8794731e.html(solidbullet的空间)
本文介绍了在Hibernate中使用不同方法填充参数的技巧,包括通过顺序占位符和引用占位符来实现参数化查询,适用于单参数及多参数场景。
193

被折叠的 条评论
为什么被折叠?



