简体   繁体   中英

Convert query from sql to hql

i want to convert this query to 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 

i tried this

@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();

but its not working

my return class is like :

public class StatisticServerDTO{

    private String status;
    private int nbr;

   ...
}

the exception is:

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: '*'

You are missing New in your query

@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 nbrServerByStatus();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM