一尘不染

两个表具有相同的列名时,出现NonUniqueDiscoveredSqlAliasException

hibernate

我有两个表Item,它们Nikasa的定义如下:

Item { id, name, spec}Nikasa {id, date, item_id}.这里item_id代表Item.id

我做了一个简单的本地SQL只能加入到选择Item.idNikasa.id为:

Session s = getSession();
SQLQuery sq = s.createSQLQuery("SELECT it.id, nik.id FROM item it LEFT JOIN nikasa nik ON (it.id = nik.item_id)");
List result = sq.list();

但是我NonUniqueDiscoveredSqlAliasException : Encountered a duplicated sql alias [ID] during auto-discovery of a native-sql query在行上遇到异常List result=sq.list();

堆栈跟踪 :

org.hibernate.loader.custom.NonUniqueDiscoveredSqlAliasException: Encountered a duplicated sql alias [ID] during auto-discovery of a native-sql query
    at org.hibernate.loader.custom.CustomLoader.autoDiscoverTypes(CustomLoader.java:594)
    at org.hibernate.loader.Loader.getResultSet(Loader.java:1986)
    at org.hibernate.loader.Loader.doQuery(Loader.java:829)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:289)
    at org.hibernate.loader.Loader.doList(Loader.java:2463)
    at org.hibernate.loader.Loader.doList(Loader.java:2449)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2279)
    at org.hibernate.loader.Loader.list(Loader.java:2274)
    at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:331)
    at org.hibernate.internal.SessionImpl.listCustomQuery(SessionImpl.java:1585)
    at org.hibernate.internal.AbstractSessionImpl.list(AbstractSessionImpl.java:224)
    at org.hibernate.internal.SQLQueryImpl.list(SQLQueryImpl.java:156)

您能建议出什么事了吗?


阅读 259

收藏
2020-06-20

共1个答案

一尘不染

您需要设置结果别名。

SELECT it.id as itemid, nik.id as nikasaid FROM item it LEFT JOIN nikasa nik ON (it.id = nik.item_id)
2020-06-20