by sequential placeholders. To populate the parameters:
1) Hibernate 2 is populated with the Session.find method
Session.find ("From TUser user where user.name=?", "Erica", hibernate.string); |
Multiple parameter conditions:
object[] args = new object[] {"Erica", New Integer (20)}; type[] types = new type{hibernate.string, Hibernate.integer}; Session.find ("From TUser user where user.name=?") And user.age=? ", args, types); |
2) parameter padding via the query interface:
Query query = Session.createquery ("From TUser user where user.name=?") and User.age>? "); Query.setstring (0, "Erica"); Query.setinteger (1, 20); |
To populate parameters by referencing placeholders:
String hql = "from TUser where Name=:name"; Query query = session.createquery (HQL); Query.setparameter ("name", "Erica"); |
How to pass a parameter of two time types to the second parameter of 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;
}
}
Above Source: http://hi.baidu.com/solidbullet/blog/item/1e1027462ada912f8794731e.html (solidbullet space)