开发者

Hibernate = Column not found

开发者 https://www.devze.com 2023-01-11 21:49 出处:网络
I am running an aggregate function in java through hibernate and for some reason it is giving me this error:

I am running an aggregate function in java through hibernate and for some reason it is giving me this error:

INFO Binary:182 - could not read column value from result set: l_date; Column 'l_date' not found. 

When I run the MySQL query the column names are l_date and logins and I can not figure out why it is not finding that.

I have tested the query in MySQL and verified that it does work. My function looks as follows.

public List<Logins> getUserLoginsByMonth() {
     Session session = getSessionFactory().openSession();
     ArrayList<Logins> loginList = null; 

     try {
          String SQL_QUERY = "SELECT l_date as l_month, SUM(logins) as logins FROM (SELECT DATE_FORMAT(login_time, '%M') as l_date, COUNT(DISTI开发者_JAVA技巧NCT users) as logins FROM user_logins WHERE login_time > DATE_SUB(NOW(), INTERVAL 1 YEAR) GROUP BY DATE(login_time)) AS Z GROUP BY(l_month)";

          Query query = session.createSQLQuery(SQL_QUERY);
          List results = query.list();
          for(ListIterator iter = results.listIterator(); iter.hasNext() ) {
                Objects[] row = (Object[])iter.next();
                System.out.println((Date)row[0}]);
                System.out.println((BigInteger)row[1]);
          }

     }
     catch(HibernateException e) {
         throw new PersistenceDaoException(e);
     }
     finally {
         session.close();
     }            
}


You need to add aliases as:

query.addScalar("l_month");
query.addScalar("logins");

and then call query.list();

It should work fine. addScalar is part of SqlQuery.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号