-->
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: select entity.map['key'] from EntityContainingMap entity ...
PostPosted: Mon Jun 19, 2006 6:24 pm 
Newbie

Joined: Wed Jun 14, 2006 7:40 pm
Posts: 8
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:

Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:

Hi all,

For sometime I've been trying to write an HQL query that is obtaining a map value based on some key in this map (without loading the entiry java.util.Map object).

Here is the real situation:

Having an I18NString as an entity that contains a map - using map element in hbm to map the java.util.Map object - index is from type java.util.Locale and the element is from type String.

I'm trying to obtain only the string from I18NString object that is from particular locale.

here is some tables :
I18NString
i18nstring_id
I18NString_Value
i18nstring_id - foreign key to I18NString table
locale - contains the locale
value - containst the string representation for this locale.

The idea is to obtain only the string that represent this object in some particular locale: something like this (note that values is the java.util.Map)

select i18nstring.values['en_US'] from I18NString i18nstring where
i18nstring.id = :i18nstringId

of course this query doesn't work (that's why I'm searching for help :) )

So my question is : Is it possible to create such HQL query or the only option that I have is to use native query ?

Thanks for your help in advanced.


Top
 Profile  
 
 Post subject: the solution
PostPosted: Tue Jun 27, 2006 8:38 pm 
Newbie

Joined: Wed Jun 14, 2006 7:40 pm
Posts: 8
OK here is the solution that I come with:

Code:
select elements(theMap)
from theEntityThatHasTheMap entity
where index(theMap) = :locale and entity.id = :id


this will select only one value from the map that is mapped to the actual :locale parameter that is passed to this query and of course the id for the map will be the actual parameter for :id

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 27, 2006 10:46 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
An alternative is to use a collection-filter. Map the i18n map normally, but require (in your own DAO logic) that the collection-filter be enabled. The filter would be on locale (= map-key), and seeing as the map already has the key specified (in the mapping from the containing object), you'll end up with a persistent map with only one item in it. This has the advantage that you don't have to run a separate query to load the localized value, but has the disadvantage that you have to write the filter and require that it always be set.

See section 17 of the ref docs for more about collection-filters.

_________________
Code tags are your friend. Know them and use them.


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.