4

我是休眠新手并尝试在休眠中运行查询,但我遇到了异常

unexpected token: ON near line 1, column 135 [SELECT A.comp_id.appRefNo ....

这是代码

StringBuffer query = new StringBuffer("SELECT A.comp_id.appRefNo, 
    A.comp_id.custId from ");

query.append(LosaCustContactZ.class.getName());

query.append(" A INNER JOIN " + LosaCust.class.getName() + " B 
    ON ( B.comp_id.appRefNo = A.comp_id.appRefNo AND " + 
    "B.comp_id.custId = A.comp_id.custId) INNER JOIN " + LosaApp.class.getName() + " C 
    ON " + "(B.comp_id.appRefNo = A.comp_id.appRefNo) ");

query.append("WHERE C.comp_id.appRefNo != ?" + " AND C.appDt >= ? AND 
    A.contactT = 'PHONE'" ); 

if (StringUtils.isNotEmpty(phoneNums)) {
    query.append(" AND A.contact IN(" + phoneNums + ")");
}

List<LosaCustContactZ> resultList = null;
try {
    resultList = getHibernateTemplate().find(query.toString(), 
           new Object[] { appRefNo, appDate });
} catch (Exception e) {
    String message = e.getMessage();
System.out.println();
}
return resultList;

我做错了什么?

谢谢

4

3 回答 3

8

许多来自 SQL 的构造不能一对一地移动到 HQL。在 HQL中,使用特定条件连接时,WITH使用关键字代替,。ON此构造特定于 Hibernate,并且不希望与其他 JPA 提供程序一起使用。

Hibernate Core 参考手册中关于 HQL 的章节,尤其是16.3 关联和连接值得一读。

于 2013-03-07T20:22:26.290 回答
3

好像您的地图中遗漏了一个映射关联hbm.xml.

请参考这里。

hbm.xml 文件中没有定义的关联

于 2013-03-07T16:51:54.080 回答
0

在更正表/列名称后尝试以下代码:

public static List<Object[]> getTopRequests(int start, int end)
        throws Exception {
    List<Object[]> list = null;
    Session session = null;

    try {
        session = HibernateUtil.openSession();

        session.beginTransaction();

        Query q = session.createQuery("SELECT "
                + " tr.id as id, "
                + // Column 0
                " tr.amount as amount,"
                + // Column 1
                " tcp.phoneNum as phoneNum, "
                + // Column 2
                " trs.faTitle as faTitle, "
                + // Column 3
                " tr.createDate as createDate "
                + // Column 4
                " FROM TopRequest as tr " + " , TopCellPhone as tcp "
                + ", TopRequestState as trs  "
                + " WHERE tcp.tcpId = tr.tcpId "
                + " AND tr.trsId = trs.trsId  "
                + " ORDER BY tr.updateDate DESC");

        q.setFirstResult(start);
        q.setMaxResults(end - start);
        list = (List<Object[]>) q.list();

        session.getTransaction().commit();

    } catch (Exception e) {
        _log.error(e.getMessage(), e);
    } finally {
        HibernateUtil.closeSession(session);
        return list;
    }
}
于 2015-09-27T08:06:55.757 回答