title: Generic enum JPA AttributeConverter implementation
body: <h1>Problem I am trying to solve</h1>
  <p>I am trying to implement enum mapping for Hibernate. So far I have researched available options, and both the <code>@Enumerated(EnumType.ORDINAL)</code> and <code>@Enumerated(EnumType.STRING)</code> seemed inadequate for my needs. The <code>@Enumerated(EnumType.ORDINAL)</code> seems to be very error-prone, as a mere reordering of enum constants can mess the mapping up, and the <code>@Enumerated(EnumType.STRING)</code> does not suffice too, as the database I work with is already full of values to be mapped, and these values are not what I would like my enum constants to be named like (the values are foreign language strings / integers).</p>
  <p>Currently, all these values are being mapped to String / Integer properties. At the same time the properties should only allow for a restricted sets of values (imagine <code>meetingStatus</code> property allowing for Strings: <code>PLANNED</code>, <code>CANCELED</code>, and <code>DONE</code>. Or another property allowing for a restricted set of Integer values: <code>1</code>, <code>2</code>, <code>3</code>, <code>4</code>, <code>5</code>).</p>
  <p>My idea was to replace the implementation with enums to improve the type safety of the code. A good example where the String / Integer implementation could cause errors is String method parameter representing such value - with String, anything goes there. Having an Enum parameter type on the other hand introduces compile time safety.</p>
  <h1>My best approach so far</h1>
  <p>The only solution that seemed to fulfill my needs was to implement custom <code>javax.persistence.AttributeConverter</code> with <code>@Converter</code> annotation for every enum. As my model would require quite a few enums, writing custom converter for each of them started to seem like a madness really quickly. So I searched for a generic solution to the problem -&gt; how to write a generic converter for any type of enum. The following answer was of big help here: <a href="https://stackoverflow.com/a/23564597/7024402">https://stackoverflow.com/a/23564597/7024402</a>. The code example in the answer provides for somewhat generic implementation, yet for every enum there is still a separate converter class needed. The author of the answer also continues:</p>
  <p><em>&quot;The alternative would be to define a custom annotation, patch the JPA provider to recognize this annotation. That way, you could examine the field type as you build the mapping information and feed the necessary enum type into a purely generic converter.&quot;</em></p>
  <p>And that's what I think I would be interested in. I could, unfortunately, not find any more information about that, and I would need a little more guidance to understand what needs to be done and how would it work with this approach.</p>
  <h1>Current Implementation</h1>
  <pre><code>public interface PersistableEnum&lt;T&gt; {
      T getValue();
  }
  </code></pre>
  <pre><code>public enum IntegerEnum implements PersistableEnum&lt;Integer&gt; {
      ONE(1),
      TWO(2),
      THREE(3),
      FOUR(4),
      FIVE(5),
      SIX(6);
  
      private int value;
  
      IntegerEnum(int value) {
          this.value = value;
      }
  
      @Override
      public Integer getValue() {
          return value;
      }
  }
  </code></pre>
  <pre><code>public abstract class PersistableEnumConverter&lt;E extends PersistableEnum&lt;T&gt;, T&gt; implements AttributeConverter&lt;E, T&gt; {
  
      private Class&lt;E&gt; enumType;
  
      public PersistableEnumConverter(Class&lt;E&gt; enumType) {
          this.enumType = enumType;
      }
  
      @Override
      public T convertToDatabaseColumn(E attribute) {
          return attribute.getValue();
      }
  
      @Override
      public E convertToEntityAttribute(T dbData) {
          for (E enumConstant : enumType.getEnumConstants()) {
              if (enumConstant.getValue().equals(dbData)) {
                  return enumConstant;
              }
          }
          throw new EnumConversionException(enumType, dbData);
      }
  }
  </code></pre>
  <pre><code>@Converter
  public class IntegerEnumConverter extends PersistableEnumConverter&lt;IntegerEnum, Integer&gt; {
  
      public IntegerEnumConverter() {
          super(IntegerEnum.class);
      }
  }
  </code></pre>
  <p>This is how I was able to achieve the partially generic converter implementation.</p>
  <p><strong>GOAL: Getting rid of the need to create new converter class for every enum.</strong></p>