hibernate高階查詢分三種查詢方式:
第一種,標準化物件查詢
主要由四部分組成:criteria、critertion、order、projection
一般查詢條件封裝示例:
criteria cri = session.createcriteria(student.class);
criterion
cron = restrictions
.like("張三",name); //查詢條件
cri.add(cron);
cri.addorder(order.asc("age")) //排序
list<
student
> list = cri.list();
聚合:
criteria cri = session.createcriteria(student.class);
cri.setprojection(projections.rowcount()); //聚合分組
cri.setprojection(projections.groupproperty("name"));
list
list = (list)cri.list();
離線查詢:
detachedcriteria dc =
detachedcriteria.forclass(student.class)
criteria cri = dc.getexecutablecriteria(session);
list<
student
> list = cri.list();
第二種,hql語言查詢(fetch一次查詢查詢出相關聯的物件)
相關示例如下:
string sql = "select * from student a ";
list<
student
> list = session.createquery(sql ).list();
string sql2 = "select * from student a inner join a.score s with s.score = 100 ";
listlist2 = session.createquery(sql2).list();
fetch和with不能同時使用
第三種,原生native sql查詢:
string sql = "select * from t_bd_student s";
sqlquery query = session.createsqlquery(sql).addentity(student.class);
listlist =
query
.list();
關聯查詢示例:
string sql = "select , from t_bd_student s join t_bd_score c on s.studentid= c.studentid";
sqlquery query = session.createsqlquery(sql).addentity("a",student.class).addentity("b",score.class);
list list = query.list();
for(int i = 0 ;iobject obj = (object)list.get(i);
student s = (student)obj[0];
score c = (
score)
obj[1];
}hibernate呼叫儲存過程
sqlquery query = session.createsqlquery("").addentity(student.class);
query.setinteger("id",1);
list list = query.list();
每日一得 20150606
mysql error 1217錯誤 error 1217 23000 cannot delete or update a parent row a foreign key constraint fails 可能是mysql在innodb中設定了foreign key關聯,造成無法更新或刪除資料。可...
每日一得 java反射
執行時載入類,使用動態載入,靜態載入出現異常,如下所示 1 使用new方式為靜態載入 public class office if excel equals args 0 結果 編譯異常 2使用反射class方式動態載入 public class officebetter catch excepti...
每日一得 openssl證書製作步驟
建立openssl環境 1.安裝 openssl 2.進入安裝目錄下的bin資料夾,把bin pem 的democa資料夾複製到bin下 3.進入democa下,新建乙個newcerts的資料夾 步驟概括 在命令列下,進入openssl的bin資料夾,1.openssl genrsa des3 ou...