本机查询SELECT多表休眠和oracle

时间:2018-08-25 07:30:45

标签: java oracle hibernate jpa

我有一个SQL查询来获取SELECT的两个表。
我想获得两个表作为结果。
这些表是某些列的名称等于(id,version)。
我有以下代码:

String sql = "select sepTemp.*,pspTerm.* " +
            "from SEPTRANSACTIONTEMP sepTemp " +
            "LEFT JOIN PSP_Terminal pspTerm ON (pspTerm.idInPSP=sepTemp.termid AND pspTerm.pspID=:pspID) " +
            "where NOT EXISTS " +
            "(select * from PSPTRANSACTION pspTrans where pspTrans.PSPID=:pspID AND pspTrans.termNo=sepTemp.rrn) ";

    List<Object[]> sepTransactionTemps = em.createNativeQuery(sql)
            .setParameter("pspID", PSPTypes.SEP.getType())
            .getResultList();

 sepTransactionTemps.forEach(row -> {
                SEPTransactionTemp sepTransactionTemp = ((SEPTransactionTemp) row[0]);
                PSPTerminal sepTransactionTemp = ((PSPTerminal) row[1]);
...

但在以下位置抛出异常:

org.hibernate.loader.custom.NonUniqueDiscoveredSqlAliasException: Encountered a duplicated sql alias [ID] during auto-discovery of a native-sql query

1 个答案:

答案 0 :(得分:0)

表中存在列ID(SEPTRANSACTIONTEMP,LEFT JOIN PSP_Terminal)
在您的选择查询中为id列创建别名

    String sql = "select " + 
            "sepTemp.id as sepTemp_id, " + 
            "sepTemp.version as sepTemp_version, " +
            "pspTerm.id as pspTerm_id, " + 
            "pspTerm.version as pspTerm_version " // try here add all your column names 
        + " from SEPTRANSACTIONTEMP sepTemp LEFT JOIN PSP_Terminal pspTerm "
        + "ON (pspTerm.idInPSP=sepTemp.termid AND pspTerm.pspID=:pspID) " + "where NOT EXISTS "
        + "(select * from PSPTRANSACTION pspTrans where pspTrans.PSPID=:pspID AND pspTrans.termNo=sepTemp.rrn) ";


List<Object[]> sepTransactionTemps = em.createNativeQuery(sql).setParameter("pspID", PSPTypes.SEP.getType())
        .getResultList();