在 JPA 中映射 i18n 表
我正在尝试使用 JPA 映射数据库(包含 60 个表)中的表。我正在为多语言应用程序执行此操作,因此每条数据都必须以多种语言提供。
我的数据库表结构是这样的。我有一个 Region 表,它与 RegionLanguage 表相关。 RegionLanguage 表实际上保存了该区域的不同语言的描述。你可能想看看这个图:
当谈到 JPA 时,我发现很难以需要尽可能少关联的方式映射它。我尝试使用辅助表概念,但在某些情况下会失败,因为这是 @OneToMany 关系。最好,我正在考虑一种解决方案,使这两个表显示为单个对象。
感谢您的帮助。
提前致谢。
I am trying to 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:
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定我是否理解这个奇怪的图表... RegionLanguage 有某种指向 Region.id 的外键,我明白吗?如果区域只有一列(如“图表”所示),您可以简单地仅映射“RegionLanguage”,并且您将只有一个所需的实体 --- 不会丢失任何信息;)。
但说真的,你希望它如何映射?你想要这样的东西吗:
或者这样的东西:
无论如何,你没有说明其余的表如何与你的 i18n 表连接。根据您的描述,我猜测所有表都与 RegionLanguage 相关。我不确定 Region 表在总体方案中的用途。我想这只是为了对语言进行分组...我想这是瑞士的“模型”(一个“地区”4 种语言)...但是您将如何处理多个地区使用的语言?您是否打算拥有多种法语、英语等语言(每个地区一种)并将所有数据乘以每种语言?
我知道你没有要求这个......我只是认为你过度简化了这个问题的数据结构......以至于很难猜测你真正想要实现什么。
无论如何,如果您想使用字符串列表方法,您可以尝试以下操作:
我仍然不明白为什么要将两个表放入一个实体中...如果它是“只读”数据,您可能会尝试创建一个视图并映射它——始终是一个“出路”;)。但在我看来,这(事实上,两者)有点过于复杂——它将来会咬你一口。我的建议是只使用“简单的一对多映射”。
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:
or something like this:
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:
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'.