-->
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.  [ 1 post ] 
Author Message
 Post subject: Wrong SQL for select with collection index
PostPosted: Thu Jun 29, 2006 5:54 am 
Newbie

Joined: Fri Apr 21, 2006 10:35 am
Posts: 13
Location: de
I use Hibernate 3.1.1 and Oracle 10.

I have a table "customer" with an ordered collection (list) "comchannels" containing phone numbers, fax numbers, e-mail addresses and the like.

This is from the code of Customer.java:
Code:
   /**
    * Referenced com channels
    * @hibernate.list cascade="all-delete-orphan" lazy="false" inverse="false"
    * @hibernate.index column="comchannel_index"
    * @hibernate.one-to-many class="ComChannel"
    * @hibernate.key foreign-key="customer_comchannel_fk"
    * @hibernate.key-column name="customer_id" index="customer_comchannel_i"
    */
   public List getComChannels() {
      return comChannels;
   }


My original query is quite big, so I simplified it and made sure I ran into the same error as before. I'm trying to construct an object from a customer's name and the value of the customer's first "Fax" ComChannel.

Here's my HQL query:
Code:
select new MyObject(
             customer.name,
            (select comchannel.value
             from ComChannel comchannel
             where comchannel = customer.comChannels[0]
               and comchannel.type = 'Fax'
            )
           )
from Customer customer
where customer.profession = 'Actor'


Hibernate generates the following syntactically incorrect SQL:
Code:
select customer0_.name as col_0_0_,
      (select comchannel1_.value
       from ComChannel comchannel1_ComChannel comchannel2_
       where customer0_.id=comchannel2_.customer_id
         and comchannel2_.com_channel_index = 0
         and comchannel1_.id=comchannel2_.id
         and comchannel1_.comchanneltype_id='Fax'
      )
      as col_1_0_
from Customer customer0_
where customer0_.profession='Actor'


As you can see, there is a comma missing in the FROM part of the subselect.

Apart from that, I'm asking myself why Hibernate uses two variables "comchannel1_" and "comchannel2_". Wouldn't one be enough? The SQL I have in mind goes like this:
Code:
select customer0_.name as col_0_0_,
      (select comchannel1_.value
       from ComChannel comchannel1_
       where customer0_.id=comchannel1_.customer_id
         and comchannel1_.com_channel_index = 0
         and comchannel1_.comchanneltype_id='Fax'
      )
      as col_1_0_
from PersonBaseData customer0_
where customer0_.profession='Actor'


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

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.