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