Search code examples
hqlhibernate-entitymanager

How to get the data in query(search result of hql)?


I am a novice to use jdbc and I have some problems.

I use hql to search data in MySQL, and the result is Query type. I don't know how to get the data from the "Query".This is my code:

final String hql = "select app.appkey,app.type from " + getClassName() +
 "app where app.appkey<>'no-appkey' group by app.type";
Query query = getEntityManager().createQuery(hql);

Thanks a lot.


Solution

  • If you are expecting a list of results, so:

    List<Object[]> results = query.getResultList();
    

    If you are expect one single result:

    Object[] result = query.getSingleResult(); // if more than one result was found, this method will throw a NonUniqueResultException
    

    If column information will be stored in a position of the Object array. Example:

    String appKey = (String) result[0];
    String appType = (String) result[1];
    

    But work with Object array is not good. Try to use Dto, like explained here.