星空网 > 软件开发 > Java

sql,hql的写法

 1 public List getList(long id){ 2     String sql="select 字段 from 对象"+" where p.id="+id; 3     Session sess=null; 4     try{ 5       sess=this.getSession(); 6       SQLQuery q = sess.createSQLQuery(sql); 7       return q.list(); 8     }finally{ 9       if(sess!=null){10         this.releaseSession(sess);11       }12     }13   }

public int delete(final long id) {      Object obj = this.getHibernateTemplate().execute(             new HibernateCallback() {                public Object doInHibernate(Session session)                      throws HibernateException, SQLException {                  String hql = "delete 对象 where id=:id";                  return new Integer(session.createQuery(hql).setLong( "id", id).executeUpdate());               }            });       return ((Integer) obj).intValue();   }

HibernateCallback是回调函数,是一个接口,该接口只有一个方法doInHibernate(org.hibernate.Session session),
这个方法体就是Spring执行的持久化操作。(上面的函数,是用hirbernate的回调函数来删除id,因为类型是int,所以最后return的时候需要转换一下类型。)
 
同理update:
public int update(final long fId,final long cId) {    Object obj=this.getHibernateTemplate().execute(        new HibernateCallback(){          public Object doInHibernate(Session session) throws HibernateException, SQLException {            String hql = "update 对象 set"+              " bId="+cId+              " where bId="+fId;            Query q=session.createQuery(hql);            return new Integer(q.executeUpdate());          }        }      );    return ((Integer)obj).intValue();  }    

public List<?> getList(boolean justEnabled){      String sql="from 对象 c";      if(justEnabled){        sql=sql+" where c.enabled='Y'";      }      return this.getHibernateTemplate().find(sql);    }

(此处的c为别名)





原标题:sql,hql的写法

关键词:sql

sql
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

新西兰专线物流:https://www.goluckyvip.com/tag/90579.html
波兰邮政:https://www.goluckyvip.com/tag/9058.html
专线西班牙:https://www.goluckyvip.com/tag/90581.html
哥伦比亚快递专线:https://www.goluckyvip.com/tag/90583.html
到马来西亚专线:https://www.goluckyvip.com/tag/90585.html
至马来西亚专线货运:https://www.goluckyvip.com/tag/90586.html
月活用户超20亿!万亿市值巨头对中国商家进一步开闸放流 :https://www.kjdsnews.com/a/1836412.html
九寨沟周围必去的景点推荐:https://www.vstour.cn/a/363190.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流