-->
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: Can't use the Map key in HQL
PostPosted: Sun May 30, 2010 12:26 pm 
Newbie

Joined: Sun Feb 14, 2010 10:13 am
Posts: 17
Hi everybody

I need help with setting my HQL selection query.

I have a Map collection in my queried entity foo:
foo.bar<barKey, barValue>

Now, in my HQL i need to search specific fields of barKey with parameter and return all foo's that hold the bars that have the correct value.

So my HQL looks like this:
Code:
select f
from foo f left join f.bar b
where index(b).barKeyField = :param


But that doesn't work! I keep getting:
Quote:
3383 [main] ERROR org.hibernate.hql.PARSER - <AST>:1:519: unexpected AST node: (
java.lang.NullPointerException
at org.hibernate.hql.ast.HqlSqlWalker.lookupProperty(HqlSqlWalker.java:546)


At the beginning i thought that maybe it's because i don't alias the index(b), so even though i don't need to return it, i added it just for investigation to the select clause:
Code:
select f, index(b) as ib
from foo f left join f.bar b
where ib.barKeyField= :param


However, that didn't work too. It couldn't relate to the field under ib. It is as if it can't work with the map key at all, only the map values, which is odd.

The HQL reference demonstrates:
Quote:
HQL also provides the built-in index() function for elements of a one-to-many association or collection of values:

select item, index(item) from Order order
join order.items item
where index(item) < 5


Does this work for anyone? how can i work with the map key to search in its fields?

Thanks,
Yuval


Top
 Profile  
 
 Post subject: Re: Can't use the Map key in HQL
PostPosted: Tue Jun 01, 2010 3:26 pm 
Newbie

Joined: Sun Feb 14, 2010 10:13 am
Posts: 17
I've solved this problem using a theta-style join:

Code:
select distinct f from foo f, bar b
where (b in indices(f.fooMap))
and (b.barKeyField = :param)


Unfortunately any regular join-based solutions failed for me.

If anyone knows another approach that uses actual joins, please share.

Yuval


Top
 Profile  
 
 Post subject: Re: Can't use the Map key in HQL
PostPosted: Tue Jun 01, 2010 3:46 pm 
Beginner
Beginner

Joined: Thu Nov 02, 2006 2:23 pm
Posts: 33
Look on hibernate tests (HQLTests)- you will find how to work with MAP joins explicit and implicit:

public void testDuplicateExplicitJoinFailureExpected() throws Exception {
//very minor issue with select clause:
assertTranslation( "from Animal a join a.mother m1 join a.mother m2" );
assertTranslation( "from Zoo zoo join zoo.animals an join zoo.mammals m" );
assertTranslation( "from Zoo zoo join zoo.mammals an join zoo.mammals m" );
}

// TESTS THAT FAIL ONLY ON DIALECTS WITH THETA-STYLE OUTERJOINS:

public void testIndexWithExplicitJoin() throws Exception {
//TODO: broken on dialects with theta-style outerjoins:
// steve (2005.10.06) - this works perfectly for me on Oracle8i
assertTranslation( "from Zoo zoo join zoo.animals an where zoo.mammals[ index(an) ] = an" );
assertTranslation( "from Zoo zoo join zoo.mammals dog where zoo.mammals[ index(dog) ] = dog" );
assertTranslation( "from Zoo zoo join zoo.mammals dog where dog = zoo.mammals[ index(dog) ]" );
}

public void testOneToManyMapIndex() throws Exception {
//TODO: this breaks on dialects with theta-style outerjoins:
// steve (2005.10.06) - this works perfectly for me on Oracle8i
assertTranslation( "from Zoo zoo where zoo.mammals['dog'].description like '%black%'" );
assertTranslation( "from Zoo zoo where zoo.mammals['dog'].father.description like '%black%'" );
assertTranslation( "from Zoo zoo where zoo.mammals['dog'].father.id = 1234" );
assertTranslation( "from Zoo zoo where zoo.animals['1234'].description like '%black%'" );
}

public void testExplicitJoinMapIndex() throws Exception {
//TODO: this breaks on dialects with theta-style outerjoins:
// steve (2005.10.06) - this works perfectly for me on Oracle8i
assertTranslation( "from Zoo zoo, Dog dog where zoo.mammals['dog'] = dog" );
assertTranslation( "from Zoo zoo join zoo.mammals dog where zoo.mammals['dog'] = dog" );
}


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.