-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Using Type(b) in the Where Clause
PostPosted: Wed Apr 27, 2016 9:23 am 
Newbie

Joined: Wed Apr 27, 2016 9:06 am
Posts: 1
FYI: using Hibernate 5.1

I have a use case where I need to filter a query by entity type, with 1..* entity types being selected.

So an example HQL would be, for selecting Cars related to User, but returning type CarTypeA (a subclass of Car) only:
Code:
SELECT DISTINCT b FROM com.zlatan.User a, com.zlatan.Car b WHERE a.id = b.user.id AND a.id = :param0 AND TYPE(b) in (com.zlatan.CarTypeA)


This query previously worked in a much older version of Hibernate (3.5.6) and the documentation for Hibernate 4 still claims to support this. However, since migrating to Hibernate 5.1 it has stopped working.
Using an in-memory Hsqldb the query returns 0 results when I would expect some.
Using an Oracle database the query actually throws an error: "ORA-00918: column ambiguously defined)"

The generated SQL roughly equates to:
Code:
SELECT DISTINCT
  car1_.OBJECT_ID AS OBJECT_I1_190_,
  car1_.NAME AS NAME3_190_,
  car1_.clazz_ AS clazz_
FROM (SELECT
  OBJECT_ID,
  NAME,
  105 AS clazz_
FROM USERS) user0_
CROSS JOIN (SELECT
  OBJECT_ID,
  NAME,
  13 AS clazz_
FROM CARDTYPEA
UNION ALL
SELECT
  OBJECT_ID,
  NAME,
  14 AS clazz_
FROM CARDTYPEB
UNION ALL
SELECT
  OBJECT_ID,
  NAME,
  15 AS clazz_
FROM CARDTYPEC) car1_
WHERE user0_.OBJECT_ID = car1_.RACK_ID
AND user0_.OBJECT_ID = ?
AND (clazz_ IN (13))


I noticed in a previous version of Hibernate the "clazz_" mentioned in the where clause would usually have an alias, such as "carent1_.clazz_".
If I manually add the alias then the SQL will run fine. So it appears the HQL is not being converted correctly? (Or at least as I would expect previously).

Has this functionality been removed from Hibernate 5 or does this appear to be a bug?


Top
 Profile  
 
 Post subject: Re: Using Type(b) in the Where Clause
PostPosted: Thu Apr 28, 2016 1:19 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
The TYPE() function is still supported by HQL, as mentioned in the 5.1 docs.

One thing I noticed is that you are using an IN clause, while the HQL query could use an equality directive.
You should also use a JOIN because it's much more efficient on the DB-side:

Code:
SELECT
    DISTINCT b
FROM com.zlatan.Car
JOIN b.user a
WHERE
    a.id = :param0 AND TYPE(b) = com.zlatan.CarTypeA


If that's not working and the alias is not properly applied, then you need to open a JIRA issue and provide a replicating test case.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.