Title: Message
This is my first contribution to this list, so please forgive me for my newbie manners
 
Apparently doing a named sql query using two or more aliases referring to the same class doesn't work as expected. 
In other words, doing something like this:
<sql-query name="myExample Query">
<return alias="a" class="my.class.A"/>
<return alias="other_a" class="my.class.A"/>
<![CDATA[
    SELECT {a.*}, {other_a.*}
    FROM MY_TABLE as a, MY_OTHER_TABLE as other_a    
    WHERE .......
]]>
</sql-query>
gives an error like this one:
net.sf.hibernate.QueryException: Alias [other_a] does not correspond to return alias a [  ....goes on with query....
Of course my specific case is much more complex than this simple example.
 
Here is what I have found and my proposal for a solution to this annoyance....
It all happens in net.sf.hibernate.loader.SQLLoader on line 144 (I am running version 2.1.3 of hibernate, it looks the same in 2.1.6 but at line 148)
 
The exception happens because, while persisters for the <return/>s are stored is a HashMap (alias2Persister) using the alias as a key and therefore the correct persister is obtained on line 130 (same in 2.6.1) by doing  SQLLoadable currentPersister = getPersisterByResultAlias(aliasName); which actually fetches the persister from the alias2Persister HashMap
, because the aliases, persisters and suffixes are stored in arrays with corresponding indexes, the program then goes on to retrieve the index by looking for the persister retrieved from line 130 in the persisters array (line 136 (line 140 in 2.1.6): int currentPersisterIndex = getPersisterIndex(currentPersister);  ), since more than one <return> entry exists for a same class, the same persister is found more than once in the persisters array, returning the wrong id for aliases and suffixes.  The index is only used for validation (causing the exception) and for retrieving the suffixes. 
 
I believe it is possible to prevent this problem by retrieving the index from searching the aliases instead of the persisters.   It requires changing line 136(line 140 in 2.1.6) (likely making the getPersisterIndex() unused) and ideally renaming the variable currentPersisterIndex  to something like currentAliasIndex. 
 
Any chance seeing this validated and fixed?
 
Thanks in advance...

Yan Laporte
Service Architecture, Loto-Québec
987-2057 #5084 (au 1801 MGill college)
[EMAIL PROTECTED]

 

Reply via email to