读书人

hibernate实施原生SQL

发布时间: 2012-07-01 13:15:00 作者: rapoo

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;            }        }));    }

读书人网 >SQL Server

热点推荐