'org.hibernate.SQLQuery' is deprecated
在Hibernate5.2之后,SQLQuery已经被摒弃,改用NativeQuery代替了。
在Hibernate中使用sql语句查询,如下所示(已略去部分方法):
//使用sql进行查询
public static void queryEmployee(){
session=getSession();
try{
transaction=session.beginTransaction();
String sql="select first_name,salary from Employee where first_name='lin'";
NativeQuery query=session.createNativeQuery(sql);
List<Object[]> workers=query.getResultList();
for(Object[] worker:workers) {
String firstName=(String) worker[0];
int salary=(int)worker[1];
System.out.println("first_name:"+firstName+" salary:"+salary);
}
}catch (Exception e) {
if(transaction!=null) {
transaction.rollback();
}
e.printStackTrace();
}finally {
session.close();
}
}