-->
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: problem with joined subtype and count (invalid alias)
PostPosted: Fri Aug 28, 2009 9:26 am 
Newbie

Joined: Fri Aug 28, 2009 8:44 am
Posts: 3
HI all,

I'm encountering a problem where I'm trying to get the numer of results a query will return (via count) but I'm not really managing to state this correctly in Hibernate.

The class layout is as follows (I've tried to simplify it a bit to make the basic problem clear):

There's a class BaseClass, it has the annotation
Code:
@Inheritance(strategy=JOINED)
on it. There's another class SubClass, that extends this BaseClass.

There's also a (Base)Reference class that associates members of BaseClass with a value class called Lit. The Reference class is also polymorphic, hence the entry_id and entry_type values below. The query I'm using is:

Code:
select
    distinct ref.base from Reference as ref
where
    ref.lit=:lit and ref.base.class = SubClass


leading to the query:

Code:
   
select distinct base_.id as base1_13_,
        base_.experiment_date as base2_13_,
        base_1_.date_created as date2_36_,
        base_1_.description as descript3_36_,
        case
            when base_1_.id is not null then 1
            when base_.id is not null then 0
        end as clazz_
    from
        references base0_
    inner join
        base base_
            on base0_.entry_id=base_.id
    left outer join
        sub base_1_
            on base_.id=base_1_.id
    where
        base0_.entry_type='BASE'
        and base0_.lit_id=?
        and case
            when base_1_.id is not null then 1
            when base_.id is not null then 0
        end=1


So I try to get the count with:
Code:
select
    count(distinct ref.base) from Reference as ref
where
    ref.lit=:lit and ref.base.class = SubClass


which gives us the query:
Code:
select
    count(distinct base0_.entry_id) as col_0_0_
from
    references base0_,
    base base_
where
    base0_.entry_type='BASE'
    and base_.entry_id=base1_.id
    and base0_.lit_id=?
    and case
        when base_1_.id is not null then 1
        when base_.id is not null then 0
    end=1

Which fails because base_1_ was never declared. Any idea what's going wrong here and how to fix it?

edit: I'm using hibernate 3.2.6


Top
 Profile  
 
 Post subject: Re: problem with joined subtype and count (invalid alias)
PostPosted: Mon Aug 31, 2009 6:03 am 
Newbie

Joined: Fri Aug 28, 2009 8:44 am
Posts: 3
I've done some thinking on this. It seems that to test inheritance hibernate will use the case statement, which relies on an outer join between base and sub. When I fetch a count of all rows though, the optimizer probably removes the outer join (or doesn't add it in the first place) as I'm not actually using any of the fields in sub. Unfortunately the case statement still gets generated as if that outer join were present.

I'm thinking this is most likely a bug and will probably report it as such. Anyone have any objections/workarounds I could try?


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.