我从SQL查询:
select e.* from terminal_remote_deployment e where id = (select top 1 e1.id from terminal_remote_deployment e1 where e1.Terminal_info_id = e.Terminal_info_id order by e1.version desc );
我试图用HQL写它:
final StringBuilder hql = new StringBuilder(); hql.append(" from TerminalRemoteDeployment e"); hql.append(" where e.id = (Select TOP 1 e1.id from TerminalRemoteDeployment e1 where e1.terminalInfo.id = e.terminalInfo.id order by e1.version desc)"); Query query = getEntityManager().createQuery(hql.toString()); resultList = (List<TerminalRemoteDeployment>) query.getResultList();
解决方法
第二个附加内部子查询中的Where子句.
它应该是terminal_Info_id而不是terminalInfo.id.
它应该是terminal_Info_id而不是terminalInfo.id.
final StringBuilder hql = new StringBuilder(); hql.append(" from TerminalRemoteDeployment e"); hql.append(" where e.id = (Select TOP 1 id from TerminalRemoteDeployment e1 where e1.terminal_Info_id = e.terminal_Info_id order by e1.version desc)"); Query query = getEntityManager().createQuery(hql.toString()); resultList = (List<TerminalRemoteDeployment>) query.getResultList();