java.lang.IllegalStateException:没有节点AggregateNode的数据类型:'MAX'

时间:2016-05-21 23:06:12

标签: java mysql hibernate hql

我正在尝试从mysql表传输

获取具有最大日期值的行

她是道

    public static Transfer getTransferByID(int id){

    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction T = null;
    Query Q = null;
    Transfer Result = null;
    Date D = null;

    try {
        T = session.beginTransaction();
        String query1 = "SELECT MAX(TDate) FROM Transfer WHERE EmpID=:id GROUP BY EmpID";
        Q = session.createQuery(query1);
        Q.setInteger("id", id);
        D = (Date) Q.uniqueResult();
        String query2 = "FROM Transfer WHERE EmpID=:id AND TDate=:dt";
        Q = session.createQuery(query2);
        Q.setInteger("id", id);
        Q.setDate("dt", D);
        Result = (Transfer) Q.uniqueResult();
        T.commit();
        return Result;

    } catch (HibernateException e) {
        if (T != null) T.rollback();
        return null;

    } catch (NullPointerException e) {
        if (T != null) T.rollback();
        return null;

    } finally {
        session.close();
    }
}

我正在使用java.sql.date 但是当我执行时,我得到了这个

    Caused by: java.lang.IllegalStateException: No data type for node: org.hibernate.hql.internal.ast.tree.AggregateNode 
 \-[AGGREGATE] AggregateNode: 'MAX'
    \-[IDENT] IdentNode: 'TDate' {originalText=TDate}
    at org.hibernate.hql.internal.ast.tree.SelectClause.initializeExplicitSelectClause(SelectClause.java:174)

我的HQL有什么问题

请帮助

1 个答案:

答案 0 :(得分:1)

请尝试使用以下别名的HQL查询:

public static Transfer getTransferByID(int id){

    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction T = null;
    Query Q = null;
    Transfer Result = null;
    Date D = null;

    try {
        T = session.beginTransaction();
        String query1 = "SELECT MAX(transfer.TDate) FROM Transfer transfer WHERE transfer.EmpID=:id GROUP BY transfer.EmpID";
        Q = session.createQuery(query1);
        Q.setInteger("id", id);
        D = (Date) Q.uniqueResult();
        String query2 = "FROM Transfer transfer WHERE transfer.EmpID=:id AND transfer.TDate=:dt";
        Q = session.createQuery(query2);
        Q.setInteger("id", id);
        Q.setDate("dt", D);
        Result = (Transfer) Q.uniqueResult();
        T.commit();
        return Result;

    } catch (HibernateException e) {
        if (T != null) T.rollback();
        return null;

    } catch (NullPointerException e) {
        if (T != null) T.rollback();
        return null;

    } finally {
        session.close();
    }
}