Hi forum!
I need some lights in order to follow my way.
I have Category entity that contains CategoryDescriptions. I've mapped them as composite-element bag collection:
Code:
<class name="...entities.Category" table="CATEGORY">
<id column="ID" name="id" type="int" unsaved-value="-1">
<generator class="identity"></generator>
</id>
...
<bag name="descriptions" table="CATEGORY_DESCRIPTION"
lazy="true">
<key column="CATEGORY_ID"></key>
<composite-element
class="com.gmsoft.middle.entities.CategoryDescription">
<many-to-one name="language"
class="com.gmsoft.middle.entities.Language" column="LANGUAGE_ID">
</many-to-one>
...
<property name="description" column="DESCRIPTION"
type="string">
</property>
</composite-element>
</bag>
</class>
<class name="...entities.Language" table="LANGUAGE">
<id column="ID" name="id" type="int" unsaved-value="-1">
<generator class="identity"></generator>
</id>
...
</class>
For example, imagine that I have a category with id = 1, and this one contains three descriptions, one in English, another in Spanish and one German. When my website is displayed to the user, the contents are only shown in one language and therefore I would like to obtain only the corresponding description (translation) when I need to access to the database.
Currently, if I get a category, the all descriptions(translations) are also retrieved with it.
So I would need to know how to filter these descriptions in order to get only the descriptions of a language. That is, obtain the Category with only one description on the collection.
I tried to implement it using HQL, but I am saturated:
Code:
public List<Category> getRootCategories(Language language) {
return this.session.createQuery("from Category cat " +
"inner join cat.descriptions descs " +
"inner join descs.language langs " +
"where langs.id = 1").list();
/*return this.session.createCriteria(Category.class)
.add(Restrictions.isNull("parent"))
.add(Restrictions.eq("descriptions.language", language))
//.add(Restrictions.eq("language", language))
.list();*/
}
Can you help me, please?
Thanks for all in advanced.