Ray_xujianguo

导航

 

  HQL是Hibernate Query Language的缩写,语法很想SQL,但是HQL是一种面向对象的查询语言。SQL的操作对象是数据列、表等数据库对象,而HQL操作的是类、实例、属性。

  HQL查询依赖于Query类,每个Query实例对应一个查询对象,使用HQL查询按如下步骤进行:

  1.获取Hibernate Session对象

  2.编写HQL语句

  3.以HQL语句作为参数,调用Session的createQuery方法创建查询对象

  4.如果HQL语句包含参数,则调用Query的setXxx方法为参数赋值

  5.调用Query独享的list()或uniqueResult()方法返回查询结果列表

简单的例子:

@SuppressWarnings("deprecation")
public class HibernateUtil {

    private static final SessionFactory sessionFactory;
    
    static {
        sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
    }
    
    public static Session getOpenSession() {
        return sessionFactory.openSession();
    }
    
    public static Session getCurrentSession() {
        return sessionFactory.getCurrentSession();
    }
}

 

@Entity
public class Employee {

    private Integer id;
    private String name;
    private Integer age;
    
    @Id
    @GeneratedValue
    public Integer getId() {
        return id;
    }
    
    public void setId(Integer id) {
        this.id = id;
    }
    
    @Basic
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    @Basic
    public Integer getAge() {
        return age;
    }
    
    public void setAge(Integer age) {
        this.age = age;
    }
    
    public String toString() {
        return "id:" + id + "   " + "name:" + name +  "   " + "age:" + age;
    }
}
@SuppressWarnings("all")
public class HQLDemo {

    @Test
    public void testHQL() {
        Session session = HibernateUtil.getOpenSession();
        List<Employee> employeeList = session.createQuery("from Employee as e").list();
        for(Employee e : employeeList)
            System.out.println(e);
    }
    
    @Test
    public void testHQLHasParameter() {
        Session session = HibernateUtil.getOpenSession();
        List<Employee> employeeList = session.createQuery("from Employee as e where e.name = :personName").setString("personName", "xujianguo").list();
        for(Employee e : employeeList)
            System.out.println(e);
    }
}

 

  HQL查询的from子句:

from是最简单的HQL语句,也是最基本的HQL语句,from关键字后紧跟持久化类的类名,如:

from Employee

表名从Employee类中选出全部的实例

不过我们常用的是这样做:

from employee as e

这个e就是Employee的别名,也就是实例名,推荐这么写。

 

  HQL查询的select子句:

  select子句用于选择指定的属性或直接选择某个实体,当然select选择的属性必须是from后持久化类包含的属性,如:

select e.name from Employee as e

  select可以选择任意属性,即不仅可以选择持久化类的直接属性,还可以选择组件属性包含的属性,如:

select e.name.firstName from Employee as e

 

  HQL查询的聚集函数:

  聚集函数:  

    avg:计算属性的平均值

    count:统计选择对象的数量

    max:统计属性值的最大值

    min:统计属性值的最小值

    sum:计算属性值的总和

如:

select count(*) from Employee as e

 

    @Test
    public void testHQLFunction() {
        Session session = HibernateUtil.getOpenSession();
        System.out.println(session.createQuery("select count(*) from Employee as e").uniqueResult());
    }

 

  多态查询:

  HQL不仅会查询出该持久化类的全部实例,还会查询出该类的子类的全部实例,前提是存在继承映射。

 

  HQL查询的where子句:

  where子句主要用于筛选选中的结果,缩小选择的范围,如:

from employee as e where e.name like "xjg%"
    @Test
    public void testHQLWhere() {
        Session session = HibernateUtil.getOpenSession();
        List<Employee> employeeList = session.createQuery("from Employee as e where e.name like 'zhou%'").list();
        for(Employee e : employeeList)
            System.out.println(e);
    }

 

  order by子句:

  查询返回结合可以根据类或组件属性的任何属性进行排序,还可以使用asc或desc关键字指定升序或者降序,如:

from Employee as e order by e.name desc

 

  子查询:

  子查询中就是查询语句中还有查询语句,如:

from Employee as e where e.age > (select p.age from Person as p)

 

    @Test
    public void testHQLChildQuery() {
        Session session = HibernateUtil.getOpenSession();
        List<Employee> employeeList = session.createQuery("from Employee as e where e.age > (select e1.age from Employee as e1 where e1.name = 'xujianguo')").list();
        for(Employee e : employeeList)
            System.out.println(e);
    }

 

  命名查询:

  HQL查询还支持将查询所用的HQL语句放入配置文件中,而不是代码中,在Hibernate映射文件的<hibernate-mapping>元素中使用<query>子元素来定义命名查询,这个<query>元素只需指定一个name属性,指定该命名查询的名字 ,如:

<query name="query">
    from Employee as e
<query />

  Session里提供了一个getNamedQuery(String name)方法,该方法用于创建一个Query对象,一旦获得Query对象,剩下的工作就跟前面的一样了。

    @Test
    public void testHQLNamedQuery() {
        Session session = HibernateUtil.getOpenSession();
        List<Employee> employeeList = session.getNamedQuery("query").list();
        for(Employee e : employeeList)
            System.out.println(e);
    }

 

 

 

 

 

posted on 2013-10-13 21:24  Ray_xujianguo  阅读(1163)  评论(10编辑  收藏  举报