简体   繁体   English

查询具有多语言列的表

[英]Querying to the table with the multiple language columns

In table object_addresses I have 3 columns (for 3 languages) name_en, name_it, name_sk.在表 object_addresses 中,我有 3 列(用于 3 种语言)name_en、name_it、name_sk。 In the future columns for other languages will be added.将来将添加其他语言的列。

Is it possible somehow to make queries SELECT name_{en, it, sk} FROM object_addresses to such a table depending on the required language, without creating 3 queries for every language and choosing proper queries in switch-case statement?是否可以根据所需的语言以某种方式查询SELECT name_{en, it, sk} FROM object_addresses到这样的表,而不为每种语言创建 3 个查询并在 switch-case 语句中选择适当的查询?

Concatenation name_ + en doesn't work.串联 name_ + en 不起作用。

I use spring-boot-starter-data-jpa version 2.4.0, PostgreSQL JDBC Driver (ver. 42.2.5, JDBC4.2)我使用 spring-boot-starter-data-jpa 版本 2.4.0,PostgreSQL JDBC 驱动程序(版本。42.2.5,JDBC4.2)

There are two ways to solve this problem:有两种方法可以解决这个问题:

  1. create your SQL dynamically.动态创建您的 SQL。 This can be as simple as using string concatenation and executing it using a JdbcTemplate .这可以像使用字符串连接并使用JdbcTemplate执行它一样简单。 Beware though that it is easy to create SQL injection vulnerabilities this way.请注意,通过这种方式很容易创建 SQL 注入漏洞。 While you didn't mention it in your question, you tagged it with .虽然您在问题中没有提到它,但您用标记了它。 Spring Data JPA doesn't offer any special mechanism for this. Spring 数据 JPA 没有为此提供任何特殊机制。 You'll have to write a custom method.您必须编写一个自定义方法。

  2. The proper way to fix this is to fix your schema.解决此问题的正确方法是修复您的架构。 When you have a situation where "future columns [like this] will be added" in 99% of the cases your schema design is broken.当您遇到 99% 的情况下“将添加未来的列 [like this]”时,您的架构设计就会被破坏。 Fix it, by extracting those texts in a separate table, referencing the original one, plus an additional key field demarcating the language.修复它,通过在单独的表格中提取这些文本,引用原始表格,加上一个额外的区分语言的关键字段。 Or alternatively the original table contains just a text_key which then is referenced in the new table, this way the new table can hold texts from various sources.或者,原始表只包含一个text_key ,然后在新表中引用它,这样新表可以保存来自各种来源的文本。 You probably don't want to navigate to these texts using entity references, but a dedicated query which will probably benefit from some serious caching.您可能不想使用实体引用导航到这些文本,而是使用可能会受益于一些严重缓存的专用查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM