How to search an enum in list of strings by postgresql query?

末鹿安然 提交于 2021-02-10 22:31:53

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!