使用HQL查詢

  HQL是Hibernate Query Language的縮寫,語法很想SQL,可是HQL是一種面向對象的查詢語言。SQL的操做對象是數據列、表等數據庫對象,而HQL操做的是類、實例、屬性。數據庫

  HQL查詢依賴於Query類,每一個Query實例對應一個查詢對象,使用HQL查詢按以下步驟進行:session

  1.獲取Hibernate Session對象app

  2.編寫HQL語句函數

  3.以HQL語句做爲參數,調用Session的createQuery方法建立查詢對象ui

  4.若是HQL語句包含參數,則調用Query的setXxx方法爲參數賦值this

  5.調用Query獨享的list()或uniqueResult()方法返回查詢結果列表spa

簡單的例子:hibernate

@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子句:code

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);
    }
相關文章
相關標籤/搜索