-
1. Re: multilingual db mapping mistery - request for help.
swd847 Mar 16, 2009 12:04 PM (in response to alex_krasov)One way of approaching this would be to use hibernate filters. This would require having a seperate row for each localised version though (there may be some way around this, but it would be a hack). You would just set the filter whaen the user logs in to only see the appropriate rows.
There is not really any automatic way to do this the way you describe in your original post, 'from MyClass' will always return every MyClass including subclasses, unless there are filters in play.
-
2. Re: multilingual db mapping mistery - request for help.
niox.nikospara.yahoo.com Mar 16, 2009 12:32 PM (in response to alex_krasov)Hello,
For the subclasses mapping problem I suggest you look at the EJB3 persistence spec, sections 2.1.9 and 2.1.10 about inheritance mapping and 9.1.10 about the AttributeOverride annotation.
Do you need to support a specific set of languages (ie default, fr, ru) or is the set open? In my opinion it is inconvenient to create a subclass per locale. In a similar occasion, where I had to support an open set of locales, I placed the multilingual fields of the entity in a different one, keyed by both entity id and locale name. Quick example (the actual code is old, EJB2 stuff, so here is a
free
translation to JPA):class Article { @Id Integer id; Date date; ... /** Maps multilingual content by locale. */ @OneToMany(...) @MapKey(name="locale") Map<String,ArticleML> mldata; ... } class ArticleML @IdClass(...) // uses composite primary key of id and locale { @Id Integer id; @Id String locale; String title; // localized content String text; // more localized content ... }
I dont know if it is the best solution though. If you only need to support a fixed and small set of languages, you could try using localized fields as:
class Article { @Id Integer id; ... String title; String titleRu; ...and so on... }
If fetching the entire multilingual content for an object is too heavy, you could experiment with lazy loading at field level.
Good luck!
-
3. Re: multilingual db mapping mistery - request for help.
alex_krasov Mar 16, 2009 1:02 PM (in response to alex_krasov)Thanks.
I thought about this kind of solution, the problem is that I wanted to be able use simple strings for localized objects. I know that subclassing for each locale is ugly but that way I tried to assign a localized string according to required locale. I did harvest the spec's inheritance, without much success for my case.
There are at least 2 more options for DB scheme - there could be one table holding all localized strings along with locale,code and type columns so they could be fetchet from there at runtime, or there could be one more localized joined table for each locale and each table (MyTable_ru in my example, for MyTable), mapped as a joined subclass.
The problem is that I neither see how to do what I'm trying to in these cases also :)