The situation I'm seeing is that when a class has a many-to-one mapping to another table multiple times, that the HQL case statement can generate improper SQL.
More specifically to my context, there is an Entity class that has FK's to a Contact table for each type of contact. The Contact class has a nullable FK to an SMSProvider class, which holds carrier/domain mappings. In my query I need to pull the 'number@domain' concatenated value, but only if the FK is no null. Otherwise just return an empty string.
Here are some env details:
Hibernate 3.1.3
JDK1.4
Oracle9Dialect
And here is the HQL I'm creating and the corresponding SQL...see how the SMSProvider table references in the where clause is placed in between the two inner join clauses...
HQL:
Code:
select
case when c1.SMSProvider is not null then concat(c1.SMSNumber,'@',c1.SMSProvider.domain) else '' end,
case when c2.SMSProvider is not null then concat(c2.SMSNumber,'@',c2.SMSProvider.domain) else '' end
from Entity e
join e.contact1 as c1
join e.contact2 as c2
where e.id in (1)
SQL:
Code:
select
case
when contact1_.SMS_PROVIDER_ID is not null then contact1_.SMSNum||'@'||smsprovide3_.SMS_PROVIDER_DOMAIN
else ''
end as col_0_0_,
case
when contact2_.SMS_PROVIDER_ID is not null then contact2_.SMSNum||'@'||smsprovide4_.SMS_PROVIDER_DOMAIN
else ''
end as col_1_0_
from ENTITY entity0_
inner join
CONTACT contact1_
on entity0_.CONTACT1_ID=contact1_.CONTACT_KEY,
SMSPROVIDER smsprovide3_
inner join
CONTACT contact2_
on entity0_.CONTACT2_ID=contact2_.CONTACT_KEY,
SMSPROVIDER smsprovide4_
where contact2_.SMS_PROVIDER_ID=smsprovide4_.SMS_PROVIDER_KEY
and contact1_.SMS_PROVIDER_ID=smsprovide3_.SMS_PROVIDER_KEY
and (
entity0_.ENTITY_KEY in (
1
)
)
Here is the exact error I'm getting:
Code:
ORA-00904: "entity0_"."CONTACT2_ID": invalid identifier
Any thoughts on this? A better way to structure the HQL? Is this simply a bug?
dlgrasse