Is there a clean solution for multilingual fields in JPA?
I think about this db structure:
table product
+---------+--------------+-----+
| Field | Type | Key |
+---------+--------------+-----+
| id | bigint(20) | PRI |
| price | varchar(255) | |
+---------+--------------+-----+
table product_lang
+------------------+--------------+-----+
| Field | Type | Key |
+------------------+--------------+-----+
| id | bigint(20) | PRI |
| lang | varchar(3) | PRI |
| title | varchar(255) | |
| description | varchar(255) | |
+------------------+--------------+-----+
And this is the class I would like to use:
@Entity
public class Product {
@Id
public Long id;
public Double price;
public Locale lang;
@Translateable
public String title;
@Translateable
public String description;
}
I’m not really sure you can map one Entity to two tables the way you propose. Similar to what you want, you may store the translations as:
where the key is the string for the Lang the user is browsing with and the value the title to show. This would create two tables in the database and allow for quick access to the translations.