HQL (五) 外置命名查询(sql语句单独保存起来)
package com.bjsxt.hibernate;
import java.util.Iterator;
import java.util.List;
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();
session.beginTransaction();
//session.createSQLQuery("sql语句"),sql语句可以是特定数据库的sql语句
List students = session.createSQLQuery("select * from t_student").list();
for (Iterator iter =students.iterator(); iter.hasNext();) {
Object[] o = (Object[])iter.next();
System.out.println(o[0] + ", " + o[1]);
}
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
}