I believe I have found a problem with an incorrectly generated query. If I have a database as follows:
create table [BDS_2_GDDEMO_VEHICLE] ([BDS_ID] numeric(19,0) not null, [VEHICLEID] nvarchar(400) not null unique, primary key ([BDS_ID]));
create table [BDS_2_GDDEMO_CAR] ([VEHICLE_BDSID] numeric(19,0) not null, [MODEL] nvarchar(400) null, [CAR_CUSTOMER_BDSID] numeric(19,0) null, [CUSTOMER_CARS_IDX] int null, primary key ([VEHICLE_BDSID]));
create table [BDS_2_GDDEMO_CUSTOMER] ([BDS_ID] numeric(19,0) not null, [NAME] nvarchar(400) null, primary key ([BDS_ID]));
create index IDX_2_GDDEMO_4D7D759B8442F2418 on [BDS_2_GDDEMO_CAR] ([CAR_CUSTOMER_BDSID]);
alter table [BDS_2_GDDEMO_CAR] add constraint FK9503290FABE5971 foreign key ([CAR_CUSTOMER_BDSID]) references [BDS_2_GDDEMO_CUSTOMER];
alter table [BDS_2_GDDEMO_CAR] add constraint FK9503290583BDD8 foreign key ([VEHICLE_BDSID]) references [BDS_2_GDDEMO_VEHICLE];
create index IDX_2_GDDEMO_50956F9F9DD406186 on [BDS_2_GDDEMO_CUSTOMER] ([NAME]);
And a hibernate mapping that looks as follows:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="things">
<class name="things.Vehicle" entity-name="things.Vehicle" abstract="false" lazy="false" table="BDS_2_GDDEMO_VEHICLE">
<id name="bdsId" column="BDS_ID" type="java.lang.Long">
<generator class="assigned"/>
</id>
<property name="vehicleID" lazy="false" insert="true" update="true" not-null="true" unique="true" type="java.lang.String">
<column not-null="true" unique="true" name="`VEHICLEID`" length="400"/>
</property>
</class>
<joined-subclass name="things.Car" entity-name="things.Car" abstract="false" lazy="false" extends="things.Vehicle" table="`BDS_2_GDDEMO_CAR`">
<key>
<column name="`VEHICLE_BDSID`"/>
</key>
<property name="model" lazy="false" insert="true" update="true" not-null="false" unique="false" type="java.lang.String">
<column not-null="false" unique="false" name="`MODEL`" length="400"/>
</property>
<many-to-one name="customer" entity-name="things.Customer" lazy="false" insert="false" update="false" not-null="false">
<column not-null="false" unique="false" name="`CAR_CUSTOMER_BDSID`" index="IDX_2_GDDEMO_F6B6B2B6980C47196"/>
</many-to-one>
</joined-subclass>
<class name="things.Customer" entity-name="things.Customer" abstract="false" lazy="false" table="`BDS_2_GDDEMO_CUSTOMER`">
<id name="bdsId" column="BDS_ID" type="java.lang.Long">
<generator class="assigned"/>
</id>
<property name="name" lazy="false" insert="true" update="true" not-null="false" unique="false" type="java.lang.String" index="IDX_2_GDDEMO_CC0954156942FC907">
<column not-null="false" unique="false" name="`NAME`" length="400" index="IDX_2_GDDEMO_CC0954156942FC907"/>
</property>
<list name="cars" lazy="true">
<key update="true">
<column name="`CAR_CUSTOMER_BDSID`" not-null="false" unique="false"/>
</key>
<list-index column="`CUSTOMER_CARS_IDX`"/>
<one-to-many entity-name="things.Car"/>
</list>
</class>
</hibernate-mapping>
Then if you try and do the following HQL select
SELECT DISTINCT co.bdsId, co.name FROM things.Customer co WHERE EXISTS (FROM co.cars t0 WHERE (t0.vehicleID = :hp0)) ORDER BY co.name, co.bdsId")
You get an error because the SQL that is generated is as follows:
select distinct customer0_.BDS_ID as col_0_0_, customer0_.[NAME] as col_1_0_ from [BDS_2_GDDEMO_CUSTOMER] customer0_ where exists (select cars1_.[VEHICLE_BDSID] from [BDS_2_GDDEMO_CAR] cars1_ where customer0_.BDS_ID=cars1_.[CAR_CUSTOMER_BDSID] and cars1_1_.[VEHICLEID]=?) order by customer0_.[NAME], customer0_.BDS_ID
The error is:
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: The multi-part identifier "cars1_1_.VEHICLEID" could not be bound.
Highlighting that the SQL that is being generated is using "cars1_1_" without actually setting it.
I have tested this on the latest Hibernate version: 4.3.8 and the problem still exists.
I have created a test case to highlight this and attached it to the report. (I have removed the hibernate libraries from the lib directory in order to keep it's size small)
|