title: Handling Regular Expressions with JPA Criteria API
body: <p>Is it possible to implement Pattern querying with JPA Criteria API?</p>
  <p>In my case, regex patterns are stored into a quick bag property; and I'm trying to avoid using native queries (e.g. PostgreSQL POSIX support).</p>
  <pre><code>@Entity @Table(name = &quot;rcp&quot;)
  public class Recipient {    
      @Id @Column(name = &quot;rcp_email_id&quot;)
      @Email
      private String email;
      @CollectionTable(joinColumns = @JoinColumn(name = &quot;rcp_email_fk&quot;))
      @ElementCollection(fetch = EAGER)
      @Convert(converter = PatternConverter.class)
      private Set&lt;Pattern&gt; rules = new HashSet&lt;&gt;();
      ...
  }
  </code></pre>
  <p>So I figured I could use the Criteria API but failed to properly develop the technlogy, obviously:</p>
  <pre><code>@AllArgsConstructor
  public class RecipientSpecification implements Specification&lt;Recipient&gt; {
      private String sample;
      @Override
      public Predicate toPredicate(Root&lt;Recipient&gt; root, CriteriaQuery&lt;?&gt; query, CriteriaBuilder builder) {
          return builder.exists(root.join(&quot;rules&quot;).as(Pattern.class).matcher(sample).find());
      }
  }
  </code></pre>
  <p>I thought I could work on the join with a cast and execute the Java Pattern logic by casting the properties, which I realize to be dumb now because it has no sense from the JPA DSL point of view. It doesn't even compile! But is there a proper way to proceed?</p>
  <p>Thanks in advance</p>