当两个表具有相同的列名时,出现NonUniqueDiscoveredSqlAliasException
技术问答
309 人阅读
|
0 人回复
|
2023-09-14
|
我有两个表Item,它们Nikasa的定义如下:
6 S( [6 M" T- e0 TItem { id, name, spec}和Nikasa {id, date, item_id}.这里item_id代表Item.id。% Z) Q+ U1 ~- Y- q: g
我做了一个简单的本地SQL只能加入到选择Item.id和Nikasa.id为:
" ~5 V8 Y' b! N: e* WSession s = getSession();
( s% w+ C, D0 w* a! C; _SQLQuery sq = s.createSQLQuery("SELECT it.id, nik.id FROM item it LEFT JOIN nikasa nik ON (it.id = nik.item_id)");
6 S& w$ |4 Z6 S9 Y( o: Y6 w: {% T. E, wList result = sq.list();' |3 |$ T3 u, E2 a, J
但是我NonUniqueDiscoveredSqlAliasException : Encountered a duplicated sql alias+ v) u- U8 I) Q+ f- N8 ~. M
[ID] during auto-discovery of a native-sql query在行上遇到异常List
7 N- }& `5 G0 d$ n) uresult=sq.list();:; H, ?* ~1 D0 p2 p2 y. u
堆栈跟踪 :
- Q- l/ a4 W1 }: f% Z5 P# Z$ `! v5 n9 forg.hibernate.loader.custom.NonUniqueDiscoveredSqlAliasException: Encountered a duplicated sql alias [ID] during auto-discovery of a native-sql query
5 ^6 i5 ~1 y4 Q) c/ Y at org.hibernate.loader.custom.CustomLoader.autoDiscoverTypes(CustomLoader.java:594)& U6 H/ y9 b" i
at org.hibernate.loader.Loader.getResultSet(Loader.java:1986)
* T. B/ X$ N6 C: b- q) T at org.hibernate.loader.Loader.doQuery(Loader.java:829)+ Q3 t9 N- A1 X& P" a6 R# Y$ U0 ~ Z
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:289)' x- j" x/ @7 c; c3 `# a1 g
at org.hibernate.loader.Loader.doList(Loader.java:2463)
- Y% R) H% l" w; R" X' c6 X8 e at org.hibernate.loader.Loader.doList(Loader.java:2449)
- K6 p+ m. r7 z' f- H0 \6 s at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2279)
4 h9 l) z+ M+ b1 K- V0 k at org.hibernate.loader.Loader.list(Loader.java:2274)
$ M( i5 V- T( N( h, h0 Q6 c at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:331)
7 w# V+ o9 y& r4 A/ `4 s at org.hibernate.internal.SessionImpl.listCustomQuery(SessionImpl.java:1585)
4 O, c, I! A# h7 |8 i at org.hibernate.internal.AbstractSessionImpl.list(AbstractSessionImpl.java:224)8 a" L0 {) {/ O+ O% G X5 `
at org.hibernate.internal.SQLQueryImpl.list(SQLQueryImpl.java:156)# v! d. ]3 t: M0 }; I
你能建议出什么事了吗?
" \# i& S6 C% }8 N
: M9 W4 Q0 u( ^" ^( |8 j7 ^( O; c5 [解决方案:
% H: x; U( p) l
8 G0 C8 e8 `) e0 b# b! ~( ?+ E! b' H/ \; [2 M6 A, h- k
! m0 e4 b4 g: b3 z0 E8 L7 }0 a- B/ C 您需要设置结果别名。3 N- P8 j0 L0 y e4 G% _. O
SELECT it.id as itemid, nik.id as nikasaid FROM item it LEFT JOIN nikasa nik ON (it.id = nik.item_id) |
|
|
|
|
|