hibernate之HQL

一  增加

getSession().save(entity)

二  删除

1 String hql = " DELETE FROM Student dm WHERE dm.id = ? ";
this.delete(hql,stu.getId());

2 Transactiontrans=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 Transactiontrans=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;
 }



非常全面的东东,太爱了,所以从别人那搬过来了。参考文章:http://blog.sina.com.cn/s/blog_99201d890101h7r5.html

你可能感兴趣的:(hibernate之HQL)