HQL檢索方式: right-hand operand of a binary operator was null
阿新 • • 發佈:2019-02-04
Hibernate Query Language
依賴介面:Query
資料模型:
tb_user(userId,userName)
tb_addr(addrId,addrName,userId)
Entity:
class Addr{
private int addrId;
private String addrName;
}
class User{
private int userId;
private String userName;
private Set addrs = new HashSet();
}
備註:省略geters 和 seters
用 ? 版本
String hSQL = "from User as u where u.userId=?"; //一般都取一個別名 as u
Query q = session.createQuery(hSQL);
q.setInt(0,1); //第一各引數為 '?' 的位置,從 0 開始,第二個引數為 ? 的值;
List users = q.list();
執行為:select * from tb_user where userId = 1;
用 :Field 版本
String hSQL = "from User as u where u.userId=:userId"; //一般都取一個別名 as u
Query q = session.createQuery(hSQL);
q.setInt("userId",1);
List users = q.list(); //注意:String hql = "from User u where u.id=? and u.name=:name";hibernate不報錯,String hql = "from User u where u.name=:name
and u.id=?";hibernate會報
嚴重: cannot define positional parameter after any named parameters have been defined
2009-4-16 15:00:33 org.hibernate.hql.ast.ErrorCounter reportError
嚴重: right-hand operand of a binary operator was null
hSQL語句例子:
以下都假以呼叫 q.list();即返回list
from:
form User as u -> select * from tb_user //返回的list裡存放的是User
select:
select u.userName from User as u where u.userId=? and u.userName like 'ab%' 返回的list裡存放的是User
select new List(u.userId,u.userName) from User as u //返回的list裡存的是 list(此list存放的對應的是userId,userName)
select new com.pannuo.entity.Test(u.userId,u.userName) from User as u //返回的list裡存的是Test
Test說明:
class Test{
private int userId;
private String userName; //必須有兩個對應屬性
public Test(int id,String name){ //必須有個對應構造方法
this.userId = id;
this.userName = name;
}
}// 省略geters 和 seters
select u.userName as name from User u //對屬性名別名
select new map(u.userName as name) from User u //返回的list裡存放的是map(以userName為key 實際選出的值為value)
聚集函式:(5種)
以下呼叫Object o = q.uniqueResult()
avg :計算屬性平均值 ->select avg(u.userId) from User u //返回Double 包裝類
count :統計選擇物件個數 ->select count(*) from User //返回Integer 或其他的
max :屬性值最大值 ->select max(u.userId) from User u //返回Integer 或其他的
min :屬性值最小值 ->select min(u.userId) from User u //返回Integer 或其他的
sum :屬性值總和 ->select sum(u.userId) from User u //返回Integer 或其他的
order by
from User u order by u.userName asc,u.userId desc //預設升序asc
group by
select a.userId,sum(a.userAge),count(a) from User u group by u.groupId
子查詢
from User as u where u.age>(select avg(b.age) from UserTmp as b)
from User as u where not(u.userName,u.age) in(select b.userName,b.age from UserTmp as b)
分頁
//從第100條開始,檢索20條資料
Query.setFirstResult(100);
Query.setMaxResult(20);
SQL查詢
SQLQuery q = (SQLQuery) session.createSQLQuery("select * from tb_user where userId=?"); //String 就為SQL語句
q.setInt(0,1);
List list = q.list();
其他
distinct //不重複
select distinct u.userName form User as u;
內聯
多對一(Addr中有一個User即:多個Addr有一個User)
以前可以用級聯操作來檢索Addr是同時檢索User
現在用內聯:
from Addr ad a inner join fetch a.user as u where c.addrId=? //內聯,同時拿Addr中的User
HQL的更新
update User u set u.userName=? where u.userId=?
例句:
select new List(d.goods,count(*)) from OrderDetail as d group by d.goods.goodsId order by count(*) desc
依賴介面:Query
資料模型:
tb_user(userId,userName)
tb_addr(addrId,addrName,userId)
Entity:
class Addr{
private int addrId;
private String addrName;
}
class User{
private int userId;
private String userName;
private Set addrs = new HashSet();
}
備註:省略geters 和 seters
用 ? 版本
String hSQL = "from User as u where u.userId=?"; //一般都取一個別名 as u
Query q = session.createQuery(hSQL);
q.setInt(0,1); //第一各引數為 '?' 的位置,從 0 開始,第二個引數為 ? 的值;
List users = q.list();
執行為:select * from tb_user where userId = 1;
用 :Field 版本
String hSQL = "from User as u where u.userId=:userId"; //一般都取一個別名 as u
Query q = session.createQuery(hSQL);
q.setInt("userId",1);
List users = q.list(); //注意:String hql = "from User u where u.id=? and u.name=:name";hibernate不報錯,String hql = "from User u where u.name=:name
and u.id=?";hibernate會報
嚴重: cannot define positional parameter after any named parameters have been defined
2009-4-16 15:00:33 org.hibernate.hql.ast.ErrorCounter reportError
嚴重: right-hand operand of a binary operator was null
hSQL語句例子:
以下都假以呼叫 q.list();即返回list
from:
form User as u -> select * from tb_user //返回的list裡存放的是User
select:
select u.userName from User as u where u.userId=? and u.userName like 'ab%' 返回的list裡存放的是User
select new List(u.userId,u.userName) from User as u //返回的list裡存的是 list(此list存放的對應的是userId,userName)
select new com.pannuo.entity.Test(u.userId,u.userName) from User as u //返回的list裡存的是Test
Test說明:
class Test{
private int userId;
private String userName; //必須有兩個對應屬性
public Test(int id,String name){ //必須有個對應構造方法
this.userId = id;
this.userName = name;
}
}// 省略geters 和 seters
select u.userName as name from User u //對屬性名別名
select new map(u.userName as name) from User u //返回的list裡存放的是map(以userName為key 實際選出的值為value)
聚集函式:(5種)
以下呼叫Object o = q.uniqueResult()
avg :計算屬性平均值 ->select avg(u.userId) from User u //返回Double 包裝類
count :統計選擇物件個數 ->select count(*) from User //返回Integer 或其他的
max :屬性值最大值 ->select max(u.userId) from User u //返回Integer 或其他的
min :屬性值最小值 ->select min(u.userId) from User u //返回Integer 或其他的
sum :屬性值總和 ->select sum(u.userId) from User u //返回Integer 或其他的
order by
from User u order by u.userName asc,u.userId desc //預設升序asc
group by
select a.userId,sum(a.userAge),count(a) from User u group by u.groupId
子查詢
from User as u where u.age>(select avg(b.age) from UserTmp as b)
from User as u where not(u.userName,u.age) in(select b.userName,b.age from UserTmp as b)
分頁
//從第100條開始,檢索20條資料
Query.setFirstResult(100);
Query.setMaxResult(20);
SQL查詢
SQLQuery q = (SQLQuery) session.createSQLQuery("select * from tb_user where userId=?"); //String 就為SQL語句
q.setInt(0,1);
List list = q.list();
其他
distinct //不重複
select distinct u.userName form User as u;
內聯
多對一(Addr中有一個User即:多個Addr有一個User)
以前可以用級聯操作來檢索Addr是同時檢索User
現在用內聯:
from Addr ad a inner join fetch a.user as u where c.addrId=? //內聯,同時拿Addr中的User
HQL的更新
update User u set u.userName=? where u.userId=?
例句:
select new List(d.goods,count(*)) from OrderDetail as d group by d.goods.goodsId order by count(*) desc