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: Querying only one line of a map
PostPosted: Wed Jan 17, 2007 3:59 pm 
Newbie

Joined: Mon Aug 14, 2006 8:06 pm
Posts: 18
Location: Montréal, Québec
Simple question, but I don't remember ever coming across an example of this.

A Country contains (among other things) a map collection of the country name in different languages:

Code:
<hibernate-mapping>
    <class name="com.example.Country" table="countries">
        <id name="id" column="id" type="int" />
        <map name="namesMap" table="countries_localized" >
            <key column="country" />
            <index column="lang" type="string" />
            <element column="name" type="string" not-null="true" />
        </map>
    </class>
</hibernate-mapping>


For example, the table countries_localized might contain:

Code:
country | lang | name
--------+------+----------------------------------
1      | 'en' | 'United States of America'
1      | 'fr' | 'États-Unis d'Amérique'
1      | 'de' | 'Vereinigten Staaten von Amerika'
1      | 'it' | 'Stati Uniti d'America'
1      | 'es' | 'Estados Unidos de América'


How do I write my query if I only want one given language, but I don't know that language at compile time?

The following obviously doesn't work (I think it's because I can't use a parameter in the FROM section of the query):

Code:
createQuery("select localizedName
from Country as country
left join country.namesMap[:lang] as localizedName
where country.id = :id")
.setParameter("id", id)
.setParameter("lang", lang);



What is the correct way of writing that query?


Hibernate version: 3.2


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 17, 2007 6:04 pm 
Newbie

Joined: Mon Aug 14, 2006 8:06 pm
Posts: 18
Location: Montréal, Québec
I just found that the same question was asked recently, without, unfortunately, any satisfactory answer:
http://forum.hibernate.org/viewtopic.php?t=968202

(And here's one similar: http://forum.hibernate.org/viewtopic.php?t=964147 )

Is there no way to avoid going to pure SQL?

(I have been using Hibernate for nearly 6 months, and the more I go, the more I find that all the time spent mapping my tables into objects was a waste. I almost always end up doing the job myself explicitely, either because Hibernate is too inefficient and too inflexible for complex queries or because the support for the collection "Map" is deficient at best... The only reason I now stick with Hibernate is because of the caches and the pools that come with it. Not very glorious, is it?)


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.