2014-02-15 41 views
-1

我是HIbernate Framework的新手,並從互聯網中挑選了一個示例代碼來開始。這裏是示例代碼Hibernate中的條件SQL查詢

DetachedCriteria criteria = DetachedCriteria.forClass(Contact.class); 
return hibernateTemplate.findByCriteria(criteria, start, limit); 

上面的代碼返回一個列表與數據庫中的所有記錄。 我的問題是什麼,如果我想寫像

select CONTACT_ID,CONTACT_EMAIL,CONTACT_NAME,CONTACT_PHONE 
from testtable 
where CONTACT_NAME='Contact12'; 
+1

有些事情需要認真學習,而不僅僅是通過挑選某些隨機站點上找到的示例。 Hibernate就是其中之一。閱讀文檔:http://hibernate.org/orm/documentation/ –

+0

順便說一句,HibernateTemplate不應該再被使用。這也寫在班上的javadoc。 –

回答

1

條件查詢,你可以使用這個具體的例子(如果該屬性被稱爲contactName

criteria.add(Restrictions.eq("contactName", "Contact12")); 

但一般而言,您應該使用其他方法Restrictions來實現這一點。