I have the following code being executed.. NumericCapability and StringCapability are subclasses of DeviceCapability.
Code:
public List findByDevice(Device device) {
List result = (List) getHibernateTemplate().find(
"select deviceCapType " +
"from DeviceCapType as deviceCapType, NumericCapability as numericCapability, " +
"StringCapability as stringCapability, Device as device " +
"where ((deviceCapType.id = numericCapability.capability.id and device.id = numericCapability.device.id) "+
" or (deviceCapType.id = stringCapability.capability.id and device.id = stringCapability.device.id)) " +
" and device.id = ? " +
"order by deviceCapType.name",
device.getId(),
Hibernate.LONG);
if (result.size() > 0) {
return result;
}
return null;
}
The query is seemingly correct in syntax, and in design -- but Hibernate prints it like this in STDOUT:
select deviceca0_.CAPABILITY_ID as CAPABILITY_ID, deviceca0_.CLASSIFICATION_ID as CLASSIFI2_,
deviceca0_.CAPABILITY_NAME as CAPABILI3_
from DEVICE_CAPABILITY_TYPE deviceca0_,
DEVICE_CAPABILITIES numericc1_, DEVICE_CAPABILITIES stringca2_, DEVICE device3_
where numericc1_.TYPE='N' and stringca2_.TYPE='S'
and (
((deviceca0_.CAPABILITY_ID=numericc1_.CAPABILITY_ID)and(device3_.DEVICE_ID=numericc1_.DEVICE_ID ))
or((deviceca0_.CAPABILITY_ID=stringca2_.CAPABILITY_ID)and(device3_.DEVICE_ID=stringca2_.DEVICE_ID ))
and(device3_.DEVICE_ID=? ))
order by deviceca0_.CAPABILITY_NAME
NB: if the volded area contained an OR instead of an AND, this query would work.
Sorry for the hibernate ugliness :)