HibernateTemplate执行普通sql语句

@Override
public List<ViewRecord> getViewRecordByUserAndRandom6(int userId) {
	final int userIdf = userId;
	List<ViewRecord> viewRecordList = this.getHibernateTemplate().executeFind(new HibernateCallback() {
	public Object doInHibernate(Session session) throws HibernateException, SQLException {
         SQLQuery query = session.createSQLQuery("select * from viewrecord where userId=? order by rand() limit 6");  
	query.setInteger(0, userIdf);
	return query.list();
			}
		});
	return viewRecordList;
	}

或者
@Override
public List<ViewRecord> getViewRecordByUserAndRandom6(int userId) {
   String sql = "select * from viewrecord where userId=? order by rand() limit 6"
   Session session = this.getSession();
   List<ViewRecord> viewRecordList = session.createSQLQuery(sql).list();
   return viewRecordList;
}

你可能感兴趣的:(java,sql)