一直一来都认为hibernate的sql操作只能操作hibernate映射文件中配置的数据库表,今天才发现不是,sqlquery可以操作数据库中任何表(不仅仅只是hibernate映射文件中映射的表)
package com.supan.test; import java.util.List; import org.hibernate.SQLQuery; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class hibernate2 { public static void main(String[] args) { Configuration cof = new Configuration().configure(); SessionFactory factory = cof.buildSessionFactory(); Session session = factory.openSession(); //注意material表并非是hibernate映射的表,而是人为随便创建的表 SQLQuery sqlquery = session.createSQLQuery("select * from material"); List result = sqlquery.list(); for(int i = 0; i < result.size(); i++) { System.out.println(result.get(0).toString()); } session.flush(); session.close(); } }