HSQLDB:遇到重复的sql别名

时间:2016-10-20 15:24:02

标签: java sql hibernate hsqldb

当我使用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执行此操作,它可以正常工作。我在该主题上找到的所有内容都没有任何帮助。任何人都知道如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

我不确定!=的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);