查詢按功能強弱可以分為以下幾種:
Native SQL > HQL > EJBQL > QBC > QBE

1、Native SQL
Native SQL為數(shù)據(jù)庫系統(tǒng)本身的SQL,里面包含了一些特有的函數(shù)等,功能也最為強大。
如:

 1    @Test
 2    public void testHQL_34() {
 3        Session session = sf.openSession();
 4        session.beginTransaction();
 5        //下面查詢使用的函數(shù)是Session的createSQLQuery
 6        SQLQuery q = session.createSQLQuery("select * from category limit 2,4").addEntity(Category.class);
 7        List<Category> categories = (List<Category>)q.list();
 8        for(Category c : categories) {
 9            System.out.println(c.getName());
10        }

11        session.getTransaction().commit();
12        session.close();
13        
14    }
2、HQL
Hibernate提供的面向?qū)ο蟛樵冋Z言。
如:
 1    @Test
 2    public void testHQL_02() {
 3        Session session = sf.openSession();
 4        session.beginTransaction();
 5        //Category是對象名而不是數(shù)據(jù)表名
 6        Query q = session.createQuery("from Category c where c.name > 'c5'");
 7        List<Category> categories = (List<Category>)q.list();
 8        for(Category c : categories) {
 9            System.out.println(c.getName());
10        }

11        session.getTransaction().commit();
12        session.close();
13        
14    }
3、EJBQL
與HQL類似,是HQL的一個子集

4、QBC
Query By Criteria,即帶約束/條件的查詢
如:
 1    @Test
 2    public void testQBC() {
 3        Session session = sf.openSession();
 4        session.beginTransaction();
 5        //criterion 標(biāo)準(zhǔn)/準(zhǔn)則/約束
 6        Criteria c = session.createCriteria(Topic.class//from Topic
 7                     
 8                     .add(Restrictions.gt("id"2)) //greater than = id > 2
 9                     .add(Restrictions.lt("id"8)) //little than = id < 8
10                     .add(Restrictions.like("title""t_"))
11                     .createCriteria("category")
12                     .add(Restrictions.between("id"35)) //category.id >= 3 and category.id <=5
13                     ;
14
15        //DetachedCriterea
16        for(Object o : c.list()) {
17            Topic t = (Topic)o;
18            System.out.println(t.getId() + "-" + t.getTitle());
19        }

20        session.getTransaction().commit();
21        session.close();
22        
23    }
5、QBE
Query By Example
如:
 1    @Test
 2    public void testQBE() {
 3        Session session = sf.openSession();
 4        session.beginTransaction();
 5        Topic tExample = new Topic();
 6        tExample.setTitle("T_");
 7        
 8        //創(chuàng)建一個例子對象,然后設(shè)置其相應(yīng)的屬性
 9        //QBE僅適合于給特定值的查詢
10        Example e = Example.create(tExample)
11                    .ignoreCase().enableLike();
12        //QBC除了添加自己的條件,最后將例子對象e也當(dāng)成條件添加進來
13        //QBC可以添加給特定值的條件,也可添加給了一定范圍的條件
14        Criteria c = session.createCriteria(Topic.class)
15                     .add(Restrictions.gt("id"2))
16                     .add(Restrictions.lt("id"8))
17                     .add(e)
18                     ;
19        //from Topic t where t.id>2 and t.id<8 and t.title like 'T_'
20                     
21        
22        for(Object o : c.list()) {
23            Topic t = (Topic)o;
24            System.out.println(t.getId() + "-" + t.getTitle());
25        }

26        session.getTransaction().commit();
27        session.close();
28        
29    }

以上幾種方法,其中QBC、QBE更加符合面向?qū)ο缶幊蹋▋H此而已)。