-->
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.  [ 6 posts ] 
Author Message
 Post subject: Collection of composite keys - can't find correct syntax
PostPosted: Tue Jun 17, 2008 7:40 am 
Newbie

Joined: Tue Jun 17, 2008 7:27 am
Posts: 6
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate Version 3.0.5

Hi Everyone,

I am unable to find the correct HQL syntax for selecting all records based on a given collection of composite keys.
My mapping files all seem to be fine, i believe the problem is with my HQL query. Would any of you please give it a more experience view.

Setup:

Class Record {
//fields
RecordKey key;
}

Class RecordKey {
String unitId;
String companyId;
String tranId;
}

Record class is mapped with a composite Key mapping in *.hbm.xml file.

Current HQL.

<![CDATA[
FROM Record rec
WHERE
rec.key.companyId ='1' AND
(rec.key) in elements(keys) ]]>

This currently throws a null pointer when trying to create a hibernate session since it seems to preparse all the named query files.

'keys' is a array/collection of RecordKey class. It wont like me use ':keys'
I set this using query.setParameter("keys",keys); in my code.
I've overridden equals + hashcode incase it needed to compare keys.

I've tried every combination of different field checks and syntax i can think of and searched Google for the last 2 days but cannot find what i'm looking for.



Thank you kindly for your help.

Jake


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 17, 2008 9:37 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
- don't use "elements" in the query
- use query.setParameterList not query.setParameter
- you'll have to use :keys or ?, keys surely won't work


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 17, 2008 10:49 am 
Newbie

Joined: Tue Jun 17, 2008 7:27 am
Posts: 6
thatmikewilliams wrote:
- don't use "elements" in the query
- use query.setParameterList not query.setParameter
- you'll have to use :keys or ?, keys surely won't work


Thanks for the aid.

If i use setParameterList and use :keys,
so "where record.key in :keys"
i receive a hibernateException Errors in named queries, unexpected token ":"

If i wrap the :keys in brackets i recieve:

Caused by: java.sql.SQLException: [SQL0104] Token , was not valid. Valid tokens: &lt; &gt; = &lt;&gt; &lt;= !&lt; !&gt; != &gt;= ¼&lt; ¼&gt; ¼= IN IS
NOT LIKE BETWEEN. Cause . . . . . : A syntax error was detected at token ,. Token , is not a valid token. A partial list of valid tokens is &lt; &
gt; = &lt;&gt; &lt;= !&lt; !&gt; != &gt;= ¼&lt; ¼&gt; ¼= IN IS NOT LIKE BETWEEN. This list assumes that the statement is correct up to the token. ......


Any further ideas?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 17, 2008 11:24 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
Quote:
Caused by: java.sql.SQLException: [SQL0104] Token , was not valid.


Sounds like progress. What was the generated SQL when you had the SQL exception?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 18, 2008 5:09 am 
Newbie

Joined: Tue Jun 17, 2008 7:27 am
Posts: 6
thatmikewilliams wrote:
Quote:
Caused by: java.sql.SQLException: [SQL0104] Token , was not valid.


Sounds like progress. What was the generated SQL when you had the SQL exception?


Thanks mikewilliams,

Here is the SQL it generated.
(ive obviously been given different names for simplicity reasons in my earlier posts)

select unittransa0_.CHDRCOY as CHDRCOY, unittransa0_.CHDRNUM as CHDRNUM, unittransa0_.TRANNO as TRANNO, unittransa0_.VRTFND as VRTFND9_, unittransa0_.UNITYP as UNITYP9_, unittransa0_.NOFDUNT as NOFDUNT9_, unittransa0_.PRICEUSED as PRICEUSED9_, unittransa0_.PRICEDT as PRICEDT9_, unittransa0_.FUNDAMNT as FUNDAMNT9_ from UTRNFBC unittransa0_ where (unittransa0_.CHDRCOY, unittransa0_.CHDRNUM, unittransa0_.TRANNO)in (?)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 18, 2008 9:10 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
This looks like a hibernate bug - invalid SQL is being generated. I haven't been able to find it in JIRA but I didn't search very hard.

Running your example on 3.0.5 and 3.2.6 the generated SQL is very different (and very wrong with 3.0.5).

Code:
3.0.5
select ... from test_record record0_ where (record0_.unitId, record0_.companyId, record0_.tranId)in (? , ?)

3.2.6
select ... from test_record record0_ where (record0_.unitId, record0_.companyId, record0_.tranId) in ((?, ?, ?) , (?, ?, ?))


Basically you need to upgrade!


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