如何通过 Querydsl 将 String[] 作为 varchar[] 传递到 PostgreSQL?

问题描述 投票:0回答:1

尝试更干净地解决Postgresql Array Functions with QueryDSL,我已经做到了这一点。

// obj.foo is an ArrayPath<String[], String>
bindings.bind(obj.foo).first((path, value) -> 
        Expressions.booleanTemplate("array_contains({0}, {1}) = true", path, value));

这最终成为看起来正确的 SQL

where array_contains(obj0_1_.foo, ?)=true

但是似乎

String[]
变量没有正确传递

org.postgresql.util.PSQLException:错误:函数 array_contains(字符变化[],bytea)不存在

我怎样才能(如果可能的话)

  1. 获取要绑定为
    String[]
    varchar[]
    值?
  2. booleanTemplate
    中表达必要的演员阵容?
postgresql hibernate spring-data-jpa querydsl
1个回答
1
投票

不要直接传递

String[]
,而是将其包裹在
TypedParameterValue
中。

hibernate-types 库尚不支持

varchar[]
,但您可以使用它来构建支持的东西:

public class VarcharArrayType extends AbstractHibernateType<String[]> {
    public static VarcharArrayType INSTANCE = new VarcharArrayType();

    public VarcharArrayType() {
        super(ArraySqlTypeDescriptor.INSTANCE, new TypeDescriptor());
    }

    public String getName() {
        return "varchar-array";
    }

    public static class TypeDescriptor extends StringArrayTypeDescriptor {
        @Override
        protected String getSqlArrayType() {
            return "varchar";
        }
    }
}

更新:在 Hibernate 6 中,这是开箱即用的。不需要额外的库、类或设置。

© www.soinside.com 2019 - 2024. All rights reserved.