Hibernate 操作sql

常用的方法:

1: Session sess = getSession();
  
  2: Session sess = this.getHibernateTemplate().getSessionFactory().openSession();
  SQLQuery  sql = sess.createSQLQuery("select coreinfoId,coreinfoName from device_coreinfo ");
  List<Object[]> sd= new ArrayList<Object[]>();
    sd = sql.list();
    sess.flush();
    sess.close();


  3:List   list=new ArrayList();
  list = (List) getHibernateTemplate().execute( 
                new HibernateCallback() { 
                    public List doInHibernate(final Session s) 
                            throws HibernateException, SQLException {  
      Query query = s.createSQLQuery(sql);//[b]执行普通sql[/b] 
    List list = query.list(); 
    return list ; 
                    } 
                });

你可能感兴趣的:(Hibernate)