0

我有以下在 SQL 软件 (HeidiSQL) 中完美运行的 SQL

SELECT gl2.documentNumber, gl2.debitAmount, gl2.creditAmount, 
 gl2.account, gl2.description FROM GeneralLedger AS gl1 
LEFT JOIN GeneralLedger AS gl2 
ON gl1.documentNumber = gl2.documentNumber 
WHERE gl1.account='911' AND gl2.account <> '911';

何时将其放入 Play!控制器,我用这个:

     String queryPL = "SELECT gl2.documentNumber, gl2.debitAmount, gl2.creditAmount, "
            + "gl2.account, gl2.description FROM GeneralLedger AS gl1 "
            + "LEFT JOIN GeneralLedger AS gl2 "
            + "ON gl1.documentNumber = gl2.documentNumber "
            + "WHERE gl1.account='911' AND gl2.account <> '911'";
    Query query = JPA.em().createQuery(queryPL);
    List<Object[]> profitAndLoss = query.getResultList();

并收到错误:

IllegalArgumentException 发生:org.hibernate.hql.ast.QuerySyntaxException:意外令牌:在第 1 行,第 156 列附近打开 [SELECT gl2.documentNumber,gl2.debitAmount,gl2.creditAmount,gl2.account,gl2.description FROM models.GeneralLedger AS gl1 LEFT JOIN GeneralLedger AS gl2 ON gl1.documentNumber = gl2.documentNumber WHERE gl1.account='911' AND gl2.account <> '911']

我将 HQL 修改为

String queryPL = "SELECT gl2.documentNumber, gl2.debitAmount, gl2.creditAmount, "
                + "gl2.account, gl2.description FROM models.GeneralLedger AS gl1 "
                + "LEFT OUTER JOIN models.GeneralLedger AS gl2 "
                + "WITH gl1.documentNumber = gl2.documentNumber "
                + "WHERE gl1.account='911' AND gl2.account <> '911'";
        Query query = JPA.em().createQuery(queryPL);
        List<Object[]> profitAndLoss = query.getResultList();

仍然收到错误:

发生 IllegalStateException:没有左侧的 DOT 节点!

那么我该如何解决这个问题呢?

4

1 回答 1

1

为了能够在 HQL 中的两个实体之间进行连接,您需要在这两个实体之间建立关联。您不能像在 SQL 中那样在任何条件下连接两个不相关的实体。Hibernate 文档中详细描述了 HQL 及其连接功能。

如果您确实需要这种类型的连接,请使用本机查询。

于 2012-04-29T17:37:32.527 回答