将查询从sql转换为hql

时间:2016-10-19 16:36:49

标签: java mysql sql hql

我想将此查询转换为HQL

select status,count(*) from server s, status st where s.id = st.server_id and st.created = (select max(ss.created) from status ss where ss.server_id = s.id) group by st.status 

我试过这个

@Query("select new StatisticServerDTO( st.status, count(*)) from Server s, Status st where "
            + "s.id = st.server.id and st.created = (select max(ss.created) from Status ss where ss.server.id = s.id)"
            + "group by st.status")
    List<StatisticServerDTO> nbrServerByStatus();

但它不起作用

我的回归课程如下:

public class StatisticServerDTO{

    private String status;
    private int nbr;

   ...
}

例外是:

Caused by: java.lang.IllegalStateException: No data type for node: org.hibernate.hql.internal.ast.tree.MethodNode 
 \-[METHOD_CALL] MethodNode: '('
    +-[METHOD_NAME] IdentNode: 'StatisticServerDTO' {originalText=StatisticServerDTO}
    \-[EXPR_LIST] SqlNode: 'exprList'
       +-[DOT] DotNode: 'status1_.status' {propertyName=status,dereferenceType=PRIMITIVE,getPropertyPath=status,path=st.status,tableAlias=status1_,className=com.example.beans.Status,classAlias=st}
       |  +-[ALIAS_REF] IdentNode: 'status1_.id' {alias=st, className=com.example.beans.Status, tableAlias=status1_}
       |  \-[IDENT] IdentNode: 'status' {originalText=status}
       \-[COUNT] CountNode: 'count'
          \-[ROW_STAR] SqlNode: '*'

1 个答案:

答案 0 :(得分:0)

您的查询中缺少新功能

@Query(从服务器s中选择 NEW StatisticServerDTO(st.status,count(*)),状态st在哪里“             +“s.id = st.server.id和st.created =(从状态ss中选择max(ss.created),其中ss.server.id = s.id)”             +“st.status小组”)     列出nbrServerByStatus();