常用的HQL語句

2021-08-29 16:40:31 字數 1212 閱讀 4214

1.hql更新

string hql = "update phuser set realname=?";

int row=this.getsession().createquery(hql).setstring(0, "小李想").executeupdate();

phuser 類名

2.hql刪除

string hql = "delete phuser a where a.userid=2";

int row=this.getsession().createquery(hql).executeupdate();

還有個這種的格式:

final string hql = "delete phrolefunction as a where a.roleid = "

+ roleid;

this.gethibernatetemplate().execute(new hibernatecallback()

});更新也可以寫成這樣的格式

3.hql單錶查詢

string hql = "from phuser a where a.userid=" + userid;

list list = this.gethibernatetemplate().find(hql);

4.hql多表查詢

(1)string hql = "select new map(a.cuid as cuid,a.unitname as unitname,b.cufid as cufid,b.ufname as ufname) from phcorrelativeunit a,phcorrelativeunitfunction b where a.cuid=b.cuid";

list list = this.gethibernatetemplate().find(hql);

多個表的字段放到map中,map的鍵值就是as後面的別名,如果沒有as就是欄位名

return this.gethibernatetemplate().find(hql);

5.得到記錄數

string hql = "select count(*) from phuser";

list list = this.gethibernatetemplate().find(hql);

return ((long) list.get(0)).intvalue();

常用的sql語句和hql語句

注 表名為students,持久化類student 一 查詢 sql select from students hql from student 二 結果排序 sql select id from students order by id desc 查詢學生的id並降序排列 hql from stu...

HIVE的常用操作(HQL 語句

hive基本操作命令 建立資料庫 create database db name create database if not exists db name 建立乙個不存在的資料庫final 檢視資料庫 show databases 選擇性檢視資料庫 show databases like f.檢視...

Hql常用語句

hql的特點 1 hql是面向實體類物件的。2 與sql大概相似,sql中的語句在hql中基本都可以用。3 hql的關鍵字不區分大小寫,但是因為是物件導向,所以類名和屬性名區分大小寫。4 在hql中select可以省略。1,簡單的查詢,user為實體名而不是資料庫中的表名 物件導向特性 hql fr...