有没有办法在下面的查询中使用HQL? SELECT userId, pwd, pwdDate FROM (SELECT userId, AES_DECRYPT(pwd, 'key_str') as pwd, pwdDate FROM UserHistory order by pwdDate desc limit 5 ) AS A WHERE pwd = :pwd
下面的方法奏效了。 SELECT * FROM UserHistory order by pwdDate desc limit 5
上面的sql可以是hibernate中的以下语句。 Criteria criteria = session.createCriteria(UserHistory.class); criteria.addOrder(Order.desc("pwdDate")); List<UserHistory> list = criteria.setMaxResults(5).list();
最佳答案
下面的方法奏效了。关键是为本机SQL创建sqlquery。
String SQL =
"SELECT A.* FROM \n" +
"(select * \n" +
" from user_history$ order by pwdDate desc limit 5 ) AS A \n" +
"where pwd = AES_ENCRYPT(:pwd, 'key_str') \n";
Query query = session.createSQLQuery(SQL);
query.setParameter("pwd", psw);
List<UserHistory> list = query.list();