getHibernateTemplate.find 有多個參數的用法

來源:互聯網
上載者:User

 通過順序預留位置。來填充參數:

    1)hibernate 2 中通過session.find方法來填充

session.find("from TUser user where user.name=?", "Erica", Hibernate.STRING);

    多個參數的情況:

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)通過Query介面進行參數填充:

Query query = session.createQuery("from TUser user where user.name=? and user.age>?");
query.setString(0,"Erica");
query.setInteger(1, 20);

    通過引用預留位置來填充參數:

String hql = "from TUser where name=:name";
Query query = session.createQuery(hql);
query.setParameter("name","Erica");

 

如何把兩個時間類型的參數傳遞到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的空間)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.