title: JPA/Hibernate custom query with list of Postgres enums
body: <p>I have an entity <code>Company</code> with type represented by enum <code>CompanyType</code>.
  Database is Postgres and it is represented as enum type there.
  I use JPA/Hibernate and its repositories. 
  Note that I am new to JPA, Hibernate and Kotlin.</p>
  
  <p>I am trying to create custom <code>@Query</code> where I need to select companies where type (the enum) is in list of possible types. I am, however, encountering various error regarding type casting in SQL and/or syntax of the <code>@Query</code>.</p>
  
  <p>Main part of the data class <code>Company</code> in Kotlin (did not copy any other attributes including id):</p>
  
  <pre><code>@Entity(name = "Company")
  @Table(name = "company")
  data class Company(
  
      @Enumerated(EnumType.STRING)
      @NotNull
      @Column(name = "type", nullable = false, columnDefinition = "company_type")
      val type: CompanyType = CompanyType.OTHER
  ) : Serializable
  </code></pre>
  
  <p>Enum <code>CompanyType</code> in Kotlin:</p>
  
  <pre><code>enum class CompanyType(value: Int) {
      BUSINESS(1),
      TOWN(2),
      NONPROFIT(3),
      RESEARCH(4),
      OTHER(5)
  }
  </code></pre>
  
  <p>Enum <code>company_type</code> in Postgres:</p>
  
  <pre><code>CREATE TYPE public.company_type AS ENUM (
      'BUSINESS',
      'TOWN',
      'NONPROFIT',
      'RESEARCH',
      'OTHER'
  );
  CREATE CAST (character varying AS public.company_type) WITH INOUT AS ASSIGNMENT;
  </code></pre>
  
  <p>JPA Repository with my initial attempt:</p>
  
  <pre><code>@Repository
  interface CompanyDAO : PagingAndSortingRepository&lt;Company&gt; {
  
      @Query("SELECT c FROM #{#entityName} c " +
          "WHERE c.type IN ?1"
      )
      fun findAllByTypeIn(types: List&lt;CompanyType&gt;, pageable: Pageable): Page&lt;Company&gt;
  }
  </code></pre>
  
  <p>which compiles but once executed following error occurs:</p>
  
  <pre><code>ERROR: operator does not exist: company_type = character varying
  </code></pre>
  
  <p>So I tried to cast it, but do not know how exactly...</p>
  
  <pre><code>    @Query("SELECT c FROM #{#entityName} c " +
          "WHERE c.type IN cast(?1 AS company_type[])"
      )
  </code></pre>
  
  <p>results in error in compile time:</p>
  
  <pre><code>antlr.MismatchedTokenException: expecting EOF, found ')'
  </code></pre>
  
  <p>and trying</p>
  
  <pre><code>    @Query("SELECT c FROM #{#entityName} c " +
          "WHERE c.type IN ?1\\:\\:company_type[]"
      )
  
  </code></pre>
  
  <p>results in</p>
  
  <pre><code>org.hibernate.QueryException: unexpected char: '\'
  </code></pre>
  
  <p>How to create such query that takes list of enums and returns such entities that have the value equal to any of items in the list?</p>