获取错误意外的标记:近线1,列135(Getting error that unexpected t

2019-08-17 09:34发布

我是新来冬眠,并试图在休眠运行查询,但是我得到的例外是

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;

我在做什么错?

谢谢

Answer 1:

好像有在你错过了一个映射关联hbm.xml.

请参考这一点。

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



Answer 2:

从SQL许多结构不能移动一个一对一的HQL。 在HQL关键字WITH是用来代替ON与特定的条件时接合,。 此结构是特定于Hibernate和不应当与其他JPA提供商合作。

关于HQL章 ,尤其是16.3协会和加入 Hibernate的核心参考手册中是值得的阅读。



Answer 3:

尝试修正表/列名后使用下面的代码:

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;
    }
}


文章来源: Getting error that unexpected token: ON near line 1, column 135
标签: hibernate hql