问题
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.
回答1:
You can cast the enum to text
handle.createQuery("select * from table where status::text in <statuses>")
.bindList("statuses", statuses)
.map(Mapper.instance)
.list());
来源:https://stackoverflow.com/questions/58550604/how-to-search-an-enum-in-list-of-strings-by-postgresql-query