hibernate执行原生sql

执行SQL插入

this.getHibernateTemplate().executeUpdate(new HibernateCallback(){  
            public Object doInHibernate(Session s) throws HibernateException,SQLException{
String sql = "insert into xxx(xxx,xxx)";
session.createQuery(sql).executeUpdate();

});
} 

更新表

this.getHibernateTemplate.merge("TableName",entity);

  

批量删除(这里是较少功能,开发中尽可能不是用循环删除)

 public String delAddressByIds(final Long[] counts) {
         return String.valueOf(this.getHibernateTemplate().execute(new HibernateCallback() {
            public String doInHibernate(Session session) throws HibernateException, SQLException {
                String spars="";
                for(int i=0;i<counts.length;i++){
                    spars=spars+counts[i]+",";
                }
                spars = spars.substring(0,spars.length()-1);
                String hql="update Store st set st.enabled=0 where st.id in ("+spars+")";
                Query q = session.createQuery(hql);
                return q.executeUpdate()>0? Constant.successCode:Constant.deleteFailed;
            }
        }));
    }

  

  

posted @ 2013-09-13 17:49  暖流  阅读(805)  评论(0编辑  收藏  举报