title: Is it possible to write a generic enum converter for JPA?
body: <p>I wanted to write a Converter for JPA that stores any enum as UPPERCASE. Some enums we encounter do not follow yet the convention to use only Uppercase letters so until they are refactored I still store the future value.</p>
  
  <p>What I got so far:</p>
  
  <pre><code>package student;
  
  public enum StudentState {
  
      Started,
      Mentoring,
      Repeating,
      STUPID,
      GENIUS;
  }
  </code></pre>
  
  <p>I want "Started" to be stored as "STARTED" and so on.</p>
  
  <pre><code>package student;
  
  import jpa.EnumUppercaseConverter;
  
  import javax.persistence.*;
  import java.io.Serializable;
  import java.util.Date;
  
  @Entity
  @Table(name = "STUDENTS")
  public class Student implements Serializable {
      private static final long serialVersionUID = 1L;
  
      @Id
      @Column(name = "ID")
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long mId;
  
      @Column(name = "LAST_NAME", length = 35)
      private String mLastName;
  
      @Column(name = "FIRST_NAME", nullable = false, length = 35)
      private String mFirstName;
  
      @Column(name = "BIRTH_DATE", nullable = false)
      @Temporal(TemporalType.DATE)
      private Date mBirthDate;
  
      @Column(name = "STUDENT_STATE")
      @Enumerated(EnumType.STRING)
      @Convert(converter = EnumUppercaseConverter.class)
      private StudentState studentState;
  
  }
  </code></pre>
  
  <p>the converter currently looks like this:</p>
  
  <pre><code>package jpa;
  
  
  import javax.persistence.AttributeConverter;
  import java.util.EnumSet;
  
  public class EnumUppercaseConverter&lt;E extends Enum&lt;E&gt;&gt; implements AttributeConverter&lt;E, String&gt; {
  
      private Class&lt;E&gt; enumClass;
  
      @Override
      public String convertToDatabaseColumn(E e) {
          return e.name().toUpperCase();
      }
  
      @Override
      public E convertToEntityAttribute(String s) {
          // which enum is it?
          for (E en : EnumSet.allOf(enumClass)) {
              if (en.name().equalsIgnoreCase(s)) {
                  return en;
              }
          }
          return null;
      }
  
  }
  </code></pre>
  
  <p>what will not work is that I do not know what enumClass will be at runtime. And I could not figure out a way to pass this information to the converter in the @Converter annotation.</p>
  
  <p>So is there a way to add parameters to the converter or cheat a bit? Or is there another way? </p>
  
  <p>I'm using EclipseLink 2.4.2</p>
  
  <p>Thanks!</p>