title: How to persist an EnumSet (using two database tables)?
body: <p>I want to represent a set of options as an EnumSet in my entity and as a one-to-many relation in my database. How is this done properly? I can only find old (pre-annotations) answers or answers not using two tables.</p>
  
  <p>I defined the following tables:</p>
  
  <pre class="lang-sql prettyprint-override"><code>CREATE TABLE Users (
      id                      SERIAL PRIMARY KEY,
      name                    VARCHAR(255) NOT NULL UNIQUE
  );
  
  CREATE TABLE User_Options (
      user_id                 INT,
      user_option             VARCHAR(255),
      PRIMARY KEY (user_id, user_option),
      FOREIGN KEY (user_id) REFERENCES Users(id)
                  ON DELETE CASCADE
                  ON UPDATE CASCADE
  );
  </code></pre>
  
  <p>this entity class:</p>
  
  <pre class="lang-java prettyprint-override"><code>@Entity(name = "Users")
  public final class User {
  
      @Id
      @GeneratedValue
      private int id;
  
      @Column(nullable = false, unique = true)
      private String name;
  
      private final Set&lt;UserOption&gt; options;
  
      {
          this.options = EnumSet.noneOf(UserOption.class);
      }
  
      /* plain getter for id included */
  
      /* plain getter and setter for name included */
  
      @ElementCollection(fetch = FetchType.EAGER)
      @Enumerated(EnumType.STRING)
      @CollectionTable(name = "User_Options"
              , joinColumns = @JoinColumn(name = "user_id"))
      @Column(name = "user_option", nullable = false)
      public Set&lt;UserOption&gt; getOptions() {
          return this.options;
      }
  
  }
  </code></pre>
  
  <p>and of course an enum:</p>
  
  <pre class="lang-java prettyprint-override"><code>public enum UserOption {
  
      OPTION_A,
      OPTION_B,
      OPTION_C;
  
  }
  </code></pre>
  
  <p>When I start Tomcat I get the following exception:</p>
  
  <pre class="lang-none prettyprint-override"><code>org.hibernate.MappingException: Could not determine type for: java.util.Set, at table: Users, for columns: [org.hibernate.mapping.Column(options)]
  </code></pre>
  
  <p>This exception is the root of a bunch of exceptions causing each other. It causes a <code>javax.persistence.PersistenceException</code> (Unable to build EntityManagerFactory), which in turn causes some dependency injection exceptions.</p>
  
  <p>I admit I'm not versed well enough in JPA/Hibernate to understand what I'm doing wrong. Can anybody help me out?</p>