-->
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.  [ 3 posts ] 
Author Message
 Post subject: Inconsistency in HQL when querying abstract classes
PostPosted: Sun Oct 28, 2012 4:15 pm 
Newbie

Joined: Sun Mar 04, 2012 1:23 pm
Posts: 7
Hi all,
I found an inconsistency and I'd like to know if it's a known bug and if there's a workaround.
I'm using Hibernate 3.6.10.

This is my entity hierarchy:

Profile <= the base entity; it has a "ProfileType" column as a discriminator
Artist <= abstract, extends Profile
SoloArtist <= extends Artist, ProfileType='SA'
BandArtist <= extends Artist, ProfileType='BA'

If I make an HQL query like this:
Code:
select artist from Artist as artist

this gets correctly translated to:
Code:
select artist0_.Id as Id5_, [...] from ProfileTable artist0_ where artist0_.ProfileType in ('SA', 'BA')

It even works for more complex cases like:
Code:
select count(artist) from Artist as artist

or when I have to put conditions on associated tables, like:
Code:
select artist from Artist as artist join artist.user as user where user.active = true

In all of these cases, the "where artist.ProfileType in ('SA', 'BA')" is always correctly generated. So far so good, it was not a so obvious use case!

However, when the abstract entity is put on the right side of a join, Hibernate does not complain, but generates an incomplete SQL. For instance, suppose an artist can write posts:
Code:
select post from Post right join post.publisher as artist where artist.active = true

(please note: the real case is more complex, so the use of the "right join" might sound strange here, but it's just to show my problem; also, don't ask yourself why here I use artist.active and not user, active, it's not important)

What I get in this case is an SQL string like the following:
Code:
select post0_.id as Id5_, [...] from PostTable post0_ right outer join ProfileTable profile_ on post0_.publisherId = profile0_.id where profile0_.active = true

That is, in this case Hibernate does not produce the "where artist.ProfileType in ('SA', 'BA')" that is needed to discriminate between a generic Profile and an Artist.

Is there a reason? A workaround?

Thanks in advance.
Mauro.


Top
 Profile  
 
 Post subject: Re: Inconsistency in HQL when querying abstract classes
PostPosted: Sun Oct 28, 2012 4:30 pm 
Newbie

Joined: Sun Mar 04, 2012 1:23 pm
Posts: 7
As a workaround I tried to use the discriminator "class" property:

Code:
select post from Post right join post.publisher as artist where artist.active = true and artist.class=Artist


Hibernate does not complain, but generates the following:

Code:
select post0_.id as Id5_, [...] from PostTable post0_ right outer join ProfileTable profile_ on post0_.publisherId = profile0_.id where profile0_.active = true and profile0_.ProfileType='fully.qualified.class.name.Artist'


which sounds like a supported use case, but handled in the wrong way.
To get the result I want, I have to write:

Code:
select post from Post as post right join post.publisher as artist where artist.active = true and (artist.class=SoloArtist or artist.class=BandArtist)


In this case, the correct where clause is generated: "(profile0_.ProfileType='SA' or profile0_.ProfileType='BA')".
However, this is not the ideal solution, since I must know in advance all the possible concrete implementations of Artist.


Last edited by mauromol on Mon Oct 29, 2012 4:11 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Inconsistency in HQL when querying abstract classes
PostPosted: Mon Oct 29, 2012 4:10 pm 
Newbie

Joined: Sun Mar 04, 2012 1:23 pm
Posts: 7
I understood why Hibernate behaves in this way... and it is reasonable. In fact, Post.owner refers generically to a Profile, not to an Artist. The fact I use the alias "artist" to refer to Post.owner isn't actually telling Hibernate it should join the post with an Artist, but just with a Profile called "artist".

So, the question is: is there a way to force Hibernate to join with an Artist, without having to explicitly select any possible concrete artist type using the "class" property?

I would have gladly used the left join instead of the right join, also because it sounds more "natural" to me; something like:

Code:
select artist from Artist as artist left join <Post... but how?> [...]


but how could I do that? The foreign key is Post.owner => Profile, I don't have the backward reference, so I don't know how to express it in HQL (if ever possible...).


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.