当我使用来自Java程序的自我联接执行此查询时

Query query = session.createSQLQuery("SELECT DISTINCT * " +
            "FROM lerneinheit AS le1 JOIN lerneinheit AS le2  " +
            "ON le1.datum = le2.datum AND le1.pid = le2.pid " +
            " WHERE " +
            "  le1.datum BETWEEN  '2016-10-20' AND '2016-10-20'  AND " +
            "  le1.pid = 3 AND " +
            "  (le1.abgesagtrechtzeitig = false OR  le1.nichtabgesagt = true OR le1.erschienen=true) AND " +
            "  (le2.abgesagtrechtzeitig = false OR  le2.nichtabgesagt = true OR le2.erschienen=true) AND " +
            "  le1.lernid!= le2.lernid AND " +
            "  (le2.beginn+1 BETWEEN le1.beginn AND le1.ende OR le2.ende-1 BETWEEN le1.beginn AND le1.ende) " +
            " ORDER BY le1.beginn");


我收到以下错误:

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


如果我从phpAdmin执行此操作,虽然效果很好。我在该主题上发现的所有内容完全没有帮助。有人知道如何解决吗?

最佳答案

我不确定,但是!=的HQL等效项是<>,所以您应该写" le1.lernid <> le2.lernid AND"

而且我建议:

Query query = session.createSQLQuery("SELECT DISTINCT * " +
        "FROM lerneinheit AS le1 JOIN lerneinheit AS le2  " +
        "ON le1.datum = le2.datum AND le1.pid = le2.pid " +
        " WHERE " +
        "  le1.datum BETWEEN  :dateMin AND :dateMax  AND " +
        "  le1.pid = :le1Pid AND " +
        "  (le1.abgesagtrechtzeitig = false OR  le1.nichtabgesagt = true OR le1.erschienen=true) AND " +
        "  (le2.abgesagtrechtzeitig = false OR  le2.nichtabgesagt = true OR le2.erschienen=true) AND " +
        "  le1.lernid!= le2.lernid AND " +
        "  (le2.beginn+1 BETWEEN le1.beginn AND le1.ende OR le2.ende-1 BETWEEN le1.beginn AND le1.ende) " +
        " ORDER BY le1.beginn");
query.setParametter("dateMin", "2016-10-20");
query.setParametter("dateMax", "2016-10-20");
query.setParametter("le1Pid", 3);

08-06 18:00