package com.metis.hibernate.test; import java.util.Iterator; import java.util.List; import org.hibernate.Criteria; import org.hibernate.Session; import org.hibernate.criterion.Restrictions; import com.metis.hibernate.domain.Person; import com.metis.hibernate.util.HibernateUtil; /** * 条件查询(Criteria queries) * HQL极为强大,但是有些人希望能够动态的使用一种面向对象API创建查询, * 而非在他们的Java代码中嵌入字符串。对于那部分人来说, * Hibernate提供了直观的Criteria查询API。 * */ public class TestCritieriaQuery { public static void main(String[] args) { Session session = HibernateUtil.openSession(); Criteria ctr = session.createCriteria(Person.class); ctr.add(Restrictions.eq("age", 23)); List list = ctr.list(); Iterator it = list.iterator(); while(it.hasNext()){ Person person = it.next(); System.out.print(" id:"+person.getId()); System.out.print(" name:"+person.getName()); System.out.println(" age:"+person.getAge()); } HibernateUtil.closeSession(); } }
Hibernate入门实例——条件查询(Criteria queries)
最新推荐文章于 2024-11-01 11:22:51 发布