简体   繁体   中英

native query SELECT multiple table hibernate and oracle

I have a SQL query for get two table of SELECT.
I want to get two table as entity in result.
these tables are some column's name is equal(id,version).
I have following code :

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]);
...

but throw exception following :

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

column id exists in your tables (SEPTRANSACTIONTEMP, LEFT JOIN PSP_Terminal)
create an alias for id column in your select query

    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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM