Hibernate 操作

List<Object[]> list = null;   
Session session = this.getHibernateTemplate().getSessionFactory().openSession();   
try {   
        list = session.createSQLQuery(sql).list();   
        for(Object[] o:list){ 
         System.out.print(((String)o[0]));    
         System.out.print(((String)o[1]));    
        }   
  
}catch(Exception e) {   
        e.printStackTrace();   
}finally{   
        session.close();   
}  

   hibernate执行原生SQL工具类:

	/**
	 * 2014-02-15增加方法
	 * ***hibernate执行原生SQL方法****
	 * @param sql
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public List queryBySql(final String sql)throws Exception{
		return (List)this.getHibernateTemplate().execute(new HibernateCallback(){
    		public Object doInHibernate(Session session) throws HibernateException, SQLException {
    			List list = session.createSQLQuery(sql).list();
    			return list;
			}	
    	}
    	);
	}

 

 

你可能感兴趣的:(sql,Hibernate)