Hi,
I am having difficulties getting Hibernate to use the correct table alias for a correlated subquery.
In the example below, I want the correlated subquery in the formula for the
lookthruTicker to reference the alias of the superclass (Security), not the outer class (Bond) for the
owner_system_code column.
Is there any way to reference the superclass?
Can I set the alias explicitly and reference that value?
Not specifying the alias defaults to the Bond table's alias
Any attempt to specify an alias (i.e. super, parent, base etc) is invalid.
Mapping documents:
Code:
<hibernate-mapping package="com.blah.db">
<class name="Security" table="security" dynamic-insert="true" dynamic-update="true" lazy="true" mutable="true">
<id name="securityId" column="security_id" type="integer" unsaved-value="null"></id>
<discriminator column="security_subclass_type" type="string"/>
<version name="version" column="version" type="int"/>
<property name="ownerSystemCode" column="owner_system_code" type="string" not-null="true" length="4"/>
<property name="ticker" column="ticker" type="string" not-null="true" length="50"/>
<subclass name="BondSecurity" discriminator-value="BOND">
<join table="bond">
<key column="security_id" on-delete="cascade"/>
<property name="someId" column="some_id" type="integer" not-null="false"/>
<property name="lookthruSomeId" column="lookthru_some_id" type="integer" not-null="false"/>
<property name="lookthruTicker"
type="string"
update="false"
insert="false"
formula="(
select s.ticker
from security s, bond b
where
b.security_id = s.security_id and
b.some_id = lookthru_some_id and
<!-- ********************************************** -->
<!-- ***** This is the line that causes grief ***** -->
s.owner_system_code = owner_system_code)"
access="field"
lazy="false"/>
</join>
</subclass>
</class>
</hibernate-mapping>
The generated SQL (show_sql=true):What this generates is
Code:
select ....
(select s.ticker
from security s, bond b
where b.security_id = s.security_id
and b.some_id = security0_1_.lookthru_some_id
and s.owner_system_code = security0_1_.owner_system_code) as formula3_0_
from security security0_
left outer join bond security0_1_ on security0_.security_id=security0_1_.security_id
where ....
The generated SQL (show_sql=true):Whereas what I want it to generate is
Code:
select ....
(select s.ticker
from security s, bond b
where b.security_id = s.security_id
and b.some_id = security0_1_.lookthru_some_id
and s.owner_system_code = security0_.owner_system_code) as formula3_0_
from security security0_
left outer join bond security0_1_ on security0_.security_id=security0_1_.security_id
where ....
The difference is very subtle. In the first example it joins to
security0_1_ and in the second example it joins to
security0_
Hibernate version: 3.1.3
Name and version of the database you are using: Sybase 12.5.3