如何通过 postgresql 查询在字符串列表中搜索枚举?

考虑一个 SQL 语句:


select * from table where status in <statuses>

其中 status 是枚举:


CREATE TYPE statusType AS ENUM (

  'enum1',

  'enum2';

在 Java 中,我有一个字符串表示形式的枚举列表:


List<String> list = new ArrayList<>();

list.add("enum1");

list.add("enum2");

然后,我尝试使用 SqlStatement 构建并执行查询:


handle.createQuery("select * from table where status in <statuses>")

                    .bindList("statuses", statuses)

                    .map(Mapper.instance)

                    .list());

我不断收到以下错误:


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.


我尝试用 CAST(enum1 as statusType) 或 enum1::statusType 包装每个列表项,但没有成功。


任何帮助表示赞赏。


料青山看我应如是
浏览 59回答 1
1回答

拉莫斯之舞

您可以将枚举转换为文本handle.createQuery("select * from table where status::text in <statuses>")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .bindList("statuses", statuses)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .map(Mapper.instance)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .list());
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java