-->
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.  [ 4 posts ] 
Author Message
 Post subject: HQL: How to query a Map atribute
PostPosted: Wed Dec 17, 2003 5:57 am 
Beginner
Beginner

Joined: Fri Nov 21, 2003 4:50 am
Posts: 23
Hi,

I have class with a property map as an attribute:

class Test {

Map extraAttributes;

/**
* @hibernate.map table="TestExtraAttributes"
* @hibernate.collection-key column="testId" type="long"
* @hibernate.collection-index column="attributeName" type="string"
* @hibernate.collection-element column="attributeValue" type="string"
*/
public Map getExtraAttributes() {
return extraAttributes;
}

}


Now I want a query to return only Test instances having special extra attributes:


select distinct test
from Test test join test.extraAttributes as extraAttributes
where extraAttributes.index='Color' and extraAttributes.element='Yellow'


The result is an exception

net.sf.hibernate.QueryException: collection of values in from clause:


What is the correct way to query a Map attribute?


I'm using Hibernate 2.0.3

Thanks

Armin


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 17, 2003 6:52 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Code:
select distinct test
from Test test
where  test.extraAttributes['Color'] = 'Yellow'


Or, in 2.1 only:

Code:
select distinct test
from Test test join test.extraAttributes as extraAttribute
where index(extraAttribute)='Color' and extraAttribute='Yellow'


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 17, 2003 7:29 am 
Beginner
Beginner

Joined: Fri Nov 21, 2003 4:50 am
Posts: 23
gavin wrote:
Code:
select distinct test
from Test test
where  test.extraAttributes['Color'] = 'Yellow'



Exactly what I need, thanks

Armin


Top
 Profile  
 
 Post subject: Re: HQL: How to query a Map atribute
PostPosted: Sat Aug 11, 2012 1:33 am 
Newbie

Joined: Thu Mar 19, 2009 9:43 am
Posts: 2
Hi

I have same requirement, but my class unfortunately has been so designed that there is no getter for the map attribute.
That is, I don't have below method

public Map getExtraAttributes() {
return extraAttributes;
}

Instead of the above I have something like below

private Map extraAttributes;

public static final String _ATTR_HOST_IP_ADDRESS = "_HOST_IP_ADDRESS";

public String getHostIPAddress() {
return (String)getAttribute(_ATTR_HOST_IP_ADDRESS);
}

public Object getAttribute (String attrKey) {
if (null == attrKey) {
return null;
}
return extraAttributes.get(attrKey);
}

So in this case how can I query a row for a particular key value pair in the map. ?

Is it a must I should have getExtraAttributes()

Pl clarify

thanks
Swayam


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