We have a requirement that our domain entities must support multiple language descriptions. We have developed a base table structure for all products that insures each product gets a unique key, then based on this key and a language id, the descriptions of the products are stored in a separate table called product_descriptions.
Therefore given a single product
ABC, I have the following in my description table:
Code:
KEY LANGUAGE LINE_NO DESCRIPTION
--- -------- ------- ----------------------------------------------------
ABC enUS 1 This is the english description of this product.
ABC enUS 2 This happens to be line 2 of the english description.
ABC esMX 1 Spanish translation here
The business' desire is that if I login with the Spanish locale, I should be shown the description in Spanish. Had I logged into the application with English, I would have been shown the two line English description. If the user had logged in using a French locale, the application wouldn't find a French translation, and should default to English instead. The caveat here is that its per-product based. Some products may have a French translation which should be shown while others may not, falling back to the English counterpart.
In the areas where I want to display the product description, I handle this by simply getting all description records from the product's getDescriptions() method and then iterate over all the results, skipping those that do not match the locale of the users session. The issue arises with this approach on large data sets where multiple calls are made back to the database for a specific records product description, which makes it highly inefficient.
Are there any constructs or methods by which I could have Hibernate do this for me when it populates the domain entity? Again I must stress, if the locale equivalent description doesn't exist, I would want the english substituted instead.
Thoughts on how I could do this aside from how I am doing it today?