In using backticks to quote an SQL Server reserved word (in an arbitrary where clause on a collection mapping), and I have found the the SQL generated by Hibernate changes.
The immediate obvious solution is to remove the backticks and change the table name, but surely this is a bug? Has anyone else come across this?
I include the generated SQL both with and without the backticks:
Hibernate version: 3.1
Mapping documents:
joined-subclasses:
Person<-Advisor<-User (User extends Advisor which extends Person)
Code:
<class name="Person" table="person">
<joined-subclass name="Advisor" table="advisor">
<key column="person_id" />
<joined-subclass name="User" table="`user`">
<key column="advisor_id" />
</joined-subclass>
</joined-subclass>
</class>
a Company has a getAdvisors() and getUsers() method:
Code:
<class name="Company" table="company">
<bag name="advisors" inverse="true" cascade="all" where="id IN (SELECT a.contact_id FROM advisor a)" >
<key column="company_id"/>
<one-to-many class="Person"/> <!-- should be class Advisor, but company_id is field on the "person" table -->
</bag>
<bag name="users" inverse="true" cascade="all" where="id IN (SELECT u.advisor_id FROM `user` u)" > <!-- note the backticks: "user" is a reserved word for SQL Server -->
<key column="company_id"/>
<one-to-many class="Person"/> <!-- should be class User, but company_id is a field on the "person" table -->
</bag>
</class>
Name and version of the database you are using: MS SQL ServerThe generated SQL (show_sql=true):SQL generated with backticks on the where clause id "IN (SELECT u.advisor_id FROM `user` u)" (mapping for getUsers() method) :
Code:
select users0_.company_id as company2_2_,
users0_.id as id2_,
users0_.id as id3_1_,
users0_.company_id as company2_3_1_,
users0_.first_name as first3_3_1_,
users0_.last_name as last4_3_1_,
users0_2_.logon as logon6_1_,
users0_2_.password as password6_1_,
case
when users0_2_.advisor_id is not null then 2
when users0_1_.person_id is not null then 1
when users0_.id is not null then 0
end as clazz_1_,
from person users0_
left outer join advisor users0_1_ on users0_.id=users0_1_.person_id
left outer join [user] users0_2_ on users0_.id=users0_2_.advisor_id
left outer join address address1_ on users0_.address_id=address1_.id
where users0_.id IN (SELECT u.advisor_id FROM users0_.[user] u) -- Why is it refering to users0_.[user]? Removing backticks solves this, but backticks are needed
and users0_.company_id=?
SQL generated without backticks on the where clause id "IN (SELECT u.advisor_id FROM user u)" :
Code:
select users0_.company_id as company2_2_,
users0_.id as id2_,
users0_.id as id3_1_,
users0_.company_id as company2_3_1_,
users0_.first_name as first3_3_1_,
users0_.last_name as last4_3_1_,
users0_2_.logon as logon6_1_,
users0_2_.password as password6_1_,
case
when users0_2_.advisor_id is not null then 2
when users0_1_.person_id is not null then 1
when users0_.id is not null then 0
end as clazz_1_,
from person users0_
left outer join advisor users0_1_ on users0_.id=users0_1_.person_id
left outer join [user] users0_2_ on users0_.id=users0_2_.advisor_id
left outer join address address1_ on users0_.address_id=address1_.id
where users0_.id IN (SELECT u.advisor_id FROM user u) -- SQL is correct, but "user" is reserved word
and users0_.company_id=?
Another solution could be:
If I could use joins instead of left outer joins, I wouldn't need to filter the results with a where clause. How do I get a set/bag mapping to use inner joins instead of outer joins?
Also, as this is a problem of polymorphism, I wondered if the class level attribute polymorphism="implicit|explicit" could help, but I find documentation on this attribute a little sparse. Are there any good articles/FAQ response?
Many thanks,