简体   繁体   English

Hibernate =未找到列

[英]Hibernate = Column not found

I am running an aggregate function in java through hibernate and for some reason it is giving me this error: 我通过hibernate在java中运行聚合函数,由于某种原因它给了我这个错误:

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. 当我运行MySQL查询时,列名是l_date和登录,我无法弄清楚为什么它没有找到它。

I have tested the query in MySQL and verified that it does work. 我在MySQL中测试了查询并验证它确实有效。 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(DISTINCT 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(); 然后调用query.list();

It should work fine. 它应该工作正常。 addScalar is part of SqlQuery. addScalar是SqlQuery的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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