3 Replies Latest reply on Mar 16, 2009 1:02 PM by alex_krasov

    multilingual db mapping mistery - request for help.

    alex_krasov

      Hi.


      I'd like to ask you for help to restore some mistery that I can't figure out for a long time:


      I have a table MyTable with integer id and string

      name

      and a few localized columns that holds a localized strings. Let they be
      name_fr

      ,
      name_ru

      , each representing localized
      name

      ...


      I'd like to map some superclass MyClass and a buch of subclasses MyClassru, MyClassen, each overriding the mapping of name property, mapping it to column appropriate for given language. I do want them to be instances of MyClass but I don't want from MyClass to return the subclasses instances, but only the MyClass ones.


      Could you please help me with this?
      Or may be there is a more natural way to solve multilingual problem? The main requirement is to be able to bring a localized strings according to given locale, transparently from user...


      Thanks a lot.



        • 1. Re: multilingual db mapping mistery - request for help.
          swd847

          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

            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
              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 :)