尝试更干净地解决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)不存在
我怎样才能(如果可能的话)
String[]
的 varchar[]
值?booleanTemplate
中表达必要的演员阵容?不要直接传递
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 中,这是开箱即用的。不需要额外的库、类或设置。