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: HQL queries using objects with composite keys
PostPosted: Thu Oct 19, 2006 10:56 pm 
Newbie

Joined: Thu Oct 19, 2006 10:39 pm
Posts: 9
Location: London
I would be grateful for any help on the fully problem:

I am attempting to perform an HQL query using an existing hibernate object as follows:


Code:
from EntitledPosition where account = :account


This is not working however as the object is linked via a composite key as follows:

Code:
<class name="EntitledPosition" table="EntitlePos">
   <composite-id>
      ...
      <key-many-to-one name="account" class="Account">
         <column name="AccountNo"/>
         <column name="HoldingCompany"/>
      </key-many-to-one>
   </composite-id>
   …
</class>


Code:
<class name="Account" table="AccountDesc">
   <composite-id>
      <key-property name="accountNo" column="AccountNo"/>
      <key-many-to-one name="HoldingCompany" class="Company" column="HoldingCompany"/>
   </composite-id>
   ...
</class>


This generates the following SQL which does not work for obvious reasons

Code:
select
   AnnouncementID,
   AccountNo,
   HoldingCompany,
   Qty
from
   EntitlePos
where
   (
          AccountNo, HoldingCompany
   )=?


Can anyone tell me why this is happening and how I can do this correctly?

If you can help me with this I will be very very grateful and forever in your debt!


Thanks,

James Bloom


Top
 Profile  
 
 Post subject: To further clarify
PostPosted: Fri Oct 20, 2006 4:59 am 
Newbie

Joined: Thu Oct 19, 2006 10:39 pm
Posts: 9
Location: London
The problem I am having is caused by Account having a composite key of AccountNo and HoldingCompany. In its self this is easy to handle in Hibernate for loading your domain object graph. The problem lies in doing queries using HQL. EntitledPosition object also has a composite key of Account and Announcement. The problem lies when I want to do a query in HQL as follows from EntitledPosition where account = :account as I understand Hibernate should know that if I want to query for EntitledPosition using an Account object it needs to retrieve the two items in Account's composite key (AccountNo and HoldingCompany) and use these to look up EntitlePostion. If this was the case I would expect the following SQL:

Code:
select
   AnnouncementID,
   AccountNo,
   HoldingCompany,
   Qty
from
   EntitlePos
where
   AccountNo = ? and
   HoldingCompany = ?


However instead Hibernate is generating the following SQL:

Code:
select
   AnnouncementID,
   AccountNo,
   HoldingCompany,
   Qty
from
   EntitlePos
where
   (
          AccountNo, HoldingCompany
   )=?


and this is not correct SQL syntax. If I change the problem so that Account only has AccountNo as its single primary key then then it works however this is not how the DB is setup.


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.