Hibernate HQL示例五:原生SQL语句查询
?使用:session.createSQLQuery
注意:这里对应的就是查询表名了..?
?
package com.bjsxt.hibernate;import java.util.Iterator;import java.util.List;import org.hibernate.SQLQuery;import org.hibernate.Session;import junit.framework.TestCase;/** * 嵌入原生sql测试 * @author Administrator * */public class SqlQueryTest extends TestCase {public void testQuery1() {Session session = null;try {session = HibernateUtils.getSession();SQLQuery sqlQuery = session.createSQLQuery("select * from t_student");List students = sqlQuery.list();for (Iterator iter = students.iterator();iter.hasNext();) {Object[] obj = (Object[])iter.next();System.out.println(obj[0] + ", " + obj[1]);}}catch(Exception e) {e.printStackTrace();}finally {HibernateUtils.closeSession(session);}}}
?