2014-02-15 40 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来实现这一点。