1

Consider a SQL Statement:

select * from table where status in <statuses>

Where status is an enum:

CREATE TYPE statusType AS ENUM (
  'enum1',
  'enum2';

In Java I have a list of the enums in string representation:

List<String> list = new ArrayList<>();
list.add("enum1");
list.add("enum2");

I then try to build and execute the query using SqlStatement:

handle.createQuery("select * from table where status in <statuses>")
                    .bindList("statuses", statuses)
                    .map(Mapper.instance)
                    .list());

I keep getting the following error:

org.jdbi.v3.core.statement.UnableToExecuteStatementException: org.postgresql.util.PSQLException: ERROR: operator does not exist: status = character varying
  Hint: No operator matches the given name and argument types. You might need to add explicit type casts.

I've tried wrapping each list item with CAST(enum1 as statusType) or enum1::statusType but no luck.

Any help is appreciated.

Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98
btbam91
  • 578
  • 2
  • 8
  • 20

1 Answers1

3

You can cast the enum to text

handle.createQuery("select * from table where status::text in <statuses>")
                .bindList("statuses", statuses)
                .map(Mapper.instance)
                .list());
Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98