开发者

Mapping i18n tables in JPA

I am trying t开发者_运维技巧o map the tables from a database (of 60 tables) using JPA. I am doing this for a multilingual application, hence every piece of data has to be available in more than one language.

My database table structure is something like this. I have a Region table, which is related to a RegionLanguage table. The RegionLanguage table actually holds the description for that Region in different languages. You may want to have a look at this diagram:

Mapping i18n tables in JPA

When it comes to JPA, I find it hard to map it in a way that would require as little associations as possible. I have tried to use the Secondary table concept, but it fails in some occasions since this is a @OneToMany relationship. Preferably, I was thinking of a solution that would make these two tables appear as a single object.

Your help is appreciated.

Thanks in advance.


I'm not sure I understand this strange diagram... The RegionLanguage has some some kind of foreign key pointing to Region.id, I take it? If Region has only one column (as shown on the 'diagram'), you can simply map only 'RegionLanguage' and you'll have only one entity as you wanted --- no information lost ;).

But seriously, how would you want it mapped? Do you want to have something like this:

class Region {
//.. the missing fields not shown in diagram
    List<String> languages;  // take only language to avoid creating separate entity for region language
}

or something like this:

class RegionInnerJoinRegionLanguage {
// all fields from Region
// all fields from RegionLanguage
}

In any case you didn't say how the rest of tables are joined with your i18n tables. From your description, I'm guessing, that all tables have fk to RegionLanguage. I'm not sure what the Region table is used for in the grand scheme of things. I guess it's just for grouping of languages... I imagine this 'models' Switzerland (one 'region' 4 languages)... But what will you do with languages spoken in several regions? Are you going to have several French, English etc. languages (one for each region) and all data multiplied for each of those??

I know you din't ask for this... I just think you oversimplified your data structure for this question... So much so that it's hard to guess what you really want to achieve.

In any case, if you want to use the list of strings approach, you can try this:

  @ElementCollection
  @CollectionTable(name="RegionLanguage",
        joinColumns=@JoinColumn(name="regionID") // or whatever... it's not on your diagram.
  )
  @Column(name="langage")
  private List<String> langages;

I still don't understand why you want to put both tables into a single entity... If it's 'read-only' data, you might try creating a view and mapping it --- always a 'way out' ;). But it's (both of them, in fact) a bit of over-complication, to my mind --- it's going to bite you in future. My advice is to just go with 'simple OneToMany mapping'.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜