Hibernate 中的hql語言的增刪改查
一 增加
getSession().save(entity)
二 刪除
1 String hql = " DELETE FROM Student dm WHERE dm.id = ? ";
this.delete(hql,stu.getId());
2 Transaction trans=session.beginTransaction();
String hql=”delete from User user where user.age=18”;
Query queryupdate=session.createQuery(hql);
int ret=queryupdate.executeUpdate();
trans.commit
三 修改
1 getSession().update(entity);
2 Transaction trans=session.beginTransaction();
String hql=”update User user set user.age=20 where user.age=18”;
Query queryupdate=session.createQuery(hql);
int ret=queryupdate.executeUpdate();
trans.commit();
四 查詢
1 String sql_0 = "from Teacher s
List listTea = createQuery(sql_0).list(); 2 List list=session.createQuery(“select user.name from User user ”).list();
for(int i=0){ System.out.println(list.get(i)); }
五 查詢通用語句
public Query createQuery(String queryString, Object... values) {
Query query = getSession().createQuery(queryString);
if (values != null) {
for (int i = 0; i < values.length; i++) {
query.setParameter(i, values[i]);
}
}
return query;
}