title: Spring data internationalization best practice
body: <p>I have a spring mvc project with spring data, jpa and hibernate. I have a multilanguage database. I designed my database and entity. I am looking for a best practice to query my tables by language. Do I have to write custom jpa query, or is there a generic way to query my tables.</p>
  
  <p>If I have a mistake on db or entity design, please warn me.</p>
  
  <p>Database:</p>
  
  <pre><code>CREATE TABLE translation (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
    PRIMARY KEY (id));
  
  CREATE TABLE translation_text (
    translation_id BIGINT UNSIGNED NOT NULL,
    lang VARCHAR(2),
    text VARCHAR(1000));
  
  ALTER TABLE translation_text
  ADD FOREIGN KEY (translation_id)
  REFERENCES translation(id);
  
  CREATE TABLE category (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
    category_name BIGINT UNSIGNED NOT NULL,
    PRIMARY KEY (id));
  
  ALTER TABLE category
  ADD FOREIGN KEY (category_name)
  REFERENCES translation(id);
  </code></pre>
  
  <p>LocalizedString Entity:</p>
  
  <pre><code>@Embeddable
  public class LocalizedString {
  
      private String lang;
      private String text;
  
      //Constructors and getter setters...
  }
  </code></pre>
  
  <p>MultilingualString Entity:</p>
  
  <pre><code>@Entity
  @Table(name = "translation")
  public class MultilingualString {
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      @Column(name = "id")
      private long id;
  
      @ElementCollection(fetch = FetchType.EAGER)
      @MapKeyColumn(name = "lang_key")
      @CollectionTable(name = "translation_text", joinColumns = @JoinColumn(name = "translation_id"))
      private final Map&lt;String, LocalizedString&gt; map = new HashMap&lt;String, LocalizedString&gt;();
  
      public MultilingualString() {
      }
  
      public MultilingualString(String lang, String text) {
          addText(lang, text);
      }
  
      public void addText(String lang, String text) {
          map.put(lang, new LocalizedString(lang, text));
      }
  
      public String getText(String lang) {
          if (map.containsKey(lang)) {
              return map.get(lang).getText();
          }
          return null;
      }
  
      public long getId() {
          return id;
      }
  
      public void setId(long id) {
          this.id = id;
      }
  }
  </code></pre>
  
  <p>Category Entity:</p>
  
  <pre><code>@Entity
  @Table(name = "category")
  @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
  public class Category extends BaseEntity&lt;Long&gt; implements Serializable {
  
      private static final long serialVersionUID = 1L;
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Column(name = "id")
      private Long id;
  
      @ManyToOne(cascade=CascadeType.ALL)
      @JoinColumn(name="category_name")
      private final MultilingualString categoryName = new MultilingualString();
  
  
      public Category(String lang, String categoryName) {
          this.categoryName.addText(lang, categoryName);
      }
      public Category() {
          super();
      }
  
      @Override
      public Long getId() {
          return id;
      }
  
      public void setId(Long id) {
          this.id = id;
      }
  
      public String getCategoryName(String lang) {
          return this.categoryName.getText(lang);
      }
  
      public void setCategoryName(String lang, String categoryName) {
          this.categoryName.addText(lang, categoryName);
      }
  
  }
  </code></pre>
  
  <p>Category Repository:</p>
  
  <pre><code>public interface CategoryRepository extends JpaRepository&lt;Category, Long&gt;{
  
  }
  </code></pre>
  
  <p>How can I pass a language parameter to <code>CategoryRepository</code> and get that language spesific data?</p>