十 Hibernate查詢之HQL多表查詢

2021-09-02 14:58:53 字數 1241 閱讀 1832

關於hql語句介紹使用可以檢視這裡,今天主要介紹其在多表系對映中的應用。

常用的sql語句多表查詢有以下幾種(以customers 和orders 表為例):

外連線查詢

多表的查詢中,hql語句和sql語句的查詢語法比較類似,hql不用書寫select * 和on及後面的條件表示式,同時表名變成類名書寫,其他關鍵字不變.

hql的多表查詢,根據返回結果劃分,可分為非迫切和迫切查詢,

/**

* 非迫切內聯查詢:查詢的客戶及其聯絡人

* sql查詢:select * from cst_customer c,cst_linkman l where c.cust_id = l.lkm_cust_id;

* hql查詢: from customer c inner join c.linkmans

*/@test

public

void

test1()

tr.commit()

;}/** * 迫切內聯查詢:查詢的客戶及其聯絡人

* sql查詢:select * from cst_customer c,cst_linkman l where c.cust_id = l.lkm_cust_id;

* hql查詢: from customer c inner join fetch c.linkmans

* * 使用fetch關鍵字,把資料封裝到物件中

*/@test

public

void

test2()

//手動解決資料重複的問題

setset =

newhashset

<

>

(list)

;for

(customer c : set)

tr.commit()

;}/** * 迫切左外查詢:查詢的客戶及其聯絡人

* sql查詢:select * from cst_customer c left join cst_linkman l on c.cust_id = l.lkm_cust_id

* hql查詢: from customer c left join fetch c.linkmans

* * 使用fetch關鍵字,把資料封裝到物件中

*/@test

public

void

test3()

tr.commit()

;}

十 Hibernate 查詢方式

hibernate 查詢方式簡介 1,導航物件圖查詢方式 2,oid 查詢方式 3,本地 sql 查詢方式 4,hql 查詢方式 5,qbc 查詢方式 query by criteria 導航物件圖查詢方式 根據已經載入的物件導航到其他物件 例如 在前面的各種對映關係中,實體類包含對其他類物件的引用...

Hibernate之批量查詢

hql查詢 string hql from student id是student 類中的屬性,不是資料庫的字段 query query seesion.createquery hql list list query.list 設定引數 string hql from student where id...

Hibernate之HQL語言查詢

建立hibernateutils類,便於直接獲取session package com.utils import org.hibernate.session import org.hibernate.sessionfactory import org.hibernate.cfg.configurat...