title: Many To Many mapping with enums
body: <p>I've got a database schema. Table provider has many to many relationships with provider_curr and provider_country (arrow from prov_curr to provider must be reverted)</p>
  
  <p><a href="https://i.stack.imgur.com/jTkrI.png" rel="nofollow noreferrer">schema</a></p>
  
  <p>class Provider,</p>
  
  <pre><code>package entity;
  
  import Currency.Country;
  import Currency.Currency;
  
  import javax.persistence.*;
  import java.util.*;
  
  @Entity
  @Table(name = "provider", schema = "public", catalog = "testdb")
  public class ProviderEntity implements Comparable&lt;ProviderEntity&gt; {
  
      private String name;
      private int id;
      private Set&lt;Currency&gt; currencies = new HashSet&lt;&gt;();
      private Set&lt;Country&gt; countries = new HashSet&lt;&gt;();
  
      public ProviderEntity() {
      }
  
  
      public Set&lt;Currency&gt; getCurrencies() {
          return currencies;
      }
  
      public void setCurrencies(Set&lt;Currency&gt; currencies) {
          this.currencies = currencies;
      }
  
  
      public Set&lt;Country&gt; getCountries() {
          return countries;
      }
  
      public void setCountries(Set&lt;Country&gt; countries) {
          this.countries = countries;
      }
  
  
  
      @Id
      @Column(name = "id", nullable = false)
      public int getId() {
          return id;
      }
  
      public void setId(int id) {
          this.id = id;
      }
  
  
      @Basic
      @Column(name = "name", nullable = false, length = 45)
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
  
  
      public ProviderEntity(String name, Set&lt;Currency&gt; currencies, Set&lt;Country&gt; countries) {
          this.name = name;
          this.currencies = currencies;
          this.countries = countries;
      }
  
  
      @Override
      public String toString() {
          return "ProviderEntity " + name + "\n Currencies: " + currencies.toString() + "\n" + "Countries" + countries.toString() + "\n";
      }
  
      public static TreeSet&lt;ProviderEntity&gt; sortByName(Set&lt;ProviderEntity&gt; values) {
          Comparator&lt;ProviderEntity&gt; comp = (ProviderEntity o1, ProviderEntity o2) -&gt; (o1.compareTo(o2));
          TreeSet&lt;ProviderEntity&gt; result = new TreeSet&lt;&gt;(comp);
          values.stream().forEach(providerEntity -&gt; result.add(providerEntity));
          System.out.println(result);
          return result;
      }
  
      public void addCurrency(String currency) {
          for (int i = 0; i &lt; Currency.values().length; i++) {
              if (Currency.values()[i].toString().equals(currency)) {
                  currencies.add(Currency.values()[i]);
              }
          }
      }
  
      public void addCountry(String country) {
          for (int i = 0; i &lt; Country.values().length; i++) {
              if (Country.values()[i].toString().equals(country)) {
                  countries.add(Country.values()[i]);
              }
          }
      }
  
      public boolean containsCurrencsy(String currency) {
          for (int i = 0; i &lt; Currency.values().length; i++) {
              if (currency.equals(Currency.values()[i].toString())) {
                  return currencies.contains(Currency.values()[i]);
              }
          }
          return false;
      }
  
      public boolean containsCountry(String country) {
          for (int i = 0; i &lt; Country.values().length; i++) {
              if (country.equals(Country.values()[i].toString())) {
                  return countries.contains(Country.values()[i]);
              }
          }
          return false;
      }
  
      @Override
      public int compareTo(ProviderEntity o) {
          return (o.name.compareTo(this.name));
      }
  }
  </code></pre>
  
  <p>enum with countries,</p>
  
  <pre><code>public enum Country {
      AUSTRALIA,
      AUSTRIA,
      AZERBAIJAN,
      ALBANIA,
      ALGERIA,
      ANGOLA,
      ARGENTINA,
      ARMENIA,
      ARUBA,
      BANGLADESH
  }
  </code></pre>
  
  <p>and enum with currencies</p>
  
  <pre><code>public enum Currency {
      DOLLAR,
      EURO,
      MANAT,
      LEK,
      DINAR,
      KWANZA,
      PESO,
      DRAM,
      GUILDER,
      TAKA
  }
  </code></pre>
  
  <p>I'm stuck with mapping Set currencies and Set countries. How can I map them?</p>
  
  <p>I tried to make it like this, but it wasn't work.</p>
  
  <pre><code>@ElementCollection(targetClass = Currency.class)
  @JoinTable(name = "provider_curr", joinColumns = @JoinColumn(name = "id"))
  @Column(name = "id", nullable = false)
  @Enumerated(EnumType.STRING)
  public Set&lt;Currency&gt; getCurrencies() {
      return currencies;
  }
  
  public void setCurrencies(Set&lt;Currency&gt; currencies) {
      this.currencies = currencies;
  }
  
  @ElementCollection(targetClass = Country.class)
  @JoinTable(name = "provider_country", joinColumns = @JoinColumn(name = "id"))
  @Column(name = "id", nullable = false)
  @Enumerated(EnumType.STRING)
  public Set&lt;Country&gt; getCountries() {
      return countries;
  }
  
  public void setCountries(Set&lt;Country&gt; countries) {
      this.countries = countries;
  }
  </code></pre>