title: Query ElementCollection of Enum by using JPA Criteria API
body: <p>I'm working of a web application for a car dealer. I have a Car class with a field which contain a set of security enums.</p>
  
  <pre><code>public class Car {
      @Id
      @GeneratedValue
      private Long id;
  
      @NotNull(message = "{year}")
      @Min(value = 1950)
      @Max(value = 2020)
      @Column(nullable = false)
      private int year;
  
      @NotNull()
      @Column(nullable = false)
      private String make;
  
      @NotNull()
      @Column(nullable = false)
      private String model;
  
      @NotNull()
      @Min(value = 0)
      @Max(value = 1000000)
      @Column(nullable = false)
      private int kilometres;
  
      @Column(nullable = false)
      private int price;
  
  
      @NotNull()
      @Enumerated(EnumType.STRING)
      private Gearbox gearbox;
  
      @ElementCollection(fetch = FetchType.EAGER)
      @Enumerated(EnumType.STRING)
      @CollectionTable(name="SECURITY")
      @Column(name="TYPE")
      private Set&lt;Security&gt; securityList = new HashSet&lt;Security&gt;();
  
      @NotNull()
      @Column(nullable = false)
      private String description;
  
      @OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY, orphanRemoval = true)
      private List&lt;Picture&gt; pictureList = new ArrayList&lt;Picture&gt;();
  
     // Getters and setters + help methods..
  </code></pre>
  
  <p>The Security enum is like:</p>
  
  <pre><code>public enum Security {
  
      ABS("abs"),
      AIRBAG("airbag"),
      ANTISPIN("antispin"),
      CENTRAL_LOCKING("centralLocking"),
      REMOTE_ALARM("remoteAlarm"),
      FOUR_WHEEL("fourWheel"),
      PARKING_ASSISTANCE("parkingAssistance"),
      SERVICE_MANUAL("serviceManual"),
      STABILITY_CONTROL("stabilityControl"),
      XENON_LIGHT("xenonLight");
  
      private String label;
  
      private Security(String label) {
  
      }
  
      public String getLabel() {
          return label;
      }
  }
  </code></pre>
  
  <p>In the web application, I will create a search page, where the users is able to define required Securitiy parts and a manufacturer pattern (make field in Car class) . For instance, a user might search for Cars which have a make pattern according to "Volkswagen" and Security with at least ABS and REMOTE_ALARM.</p>
  
  <p>My problem is that I am not sure how to create the query using the criteria API. I guess it should start like:</p>
  
  <pre><code>public List&lt;Car&gt; searchCars(String makePattern, Set&lt;Security&gt; requiredSecuirtySet) {
  
          CriteriaBuilder cb = em.getCriteriaBuilder();
  
          CriteriaQuery&lt;Car&gt; cq = cb.createQuery(Car.class);
          Root&lt;Car&gt; _car = cq.from(Car.class);
  
  
          // Give me some help here please =)
  
  
          return em.createQuery(cq).getResultList();
  }
  </code></pre>
  
  <p>Can you please help me? I also have a meta model over the Car class.</p>
  
  <p>Best regards and thanks in advance!</p>