0

Trying to solve Postgresql Array Functions with QueryDSL more cleanly, I've got this far.

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

this ends up as correct-looking SQL

where arraycontains(obj0_1_.foo, ?)=true

but it seems the String[] variable is not passed correctly

org.postgresql.util.PSQLException: ERROR: function arraycontains(character varying[], bytea) does not exist

How can I either (if possible)

  1. get the String[] value to bind as a varchar[]?
  2. express the necessary cast in the booleanTemplate?
OrangeDog
  • 36,653
  • 12
  • 122
  • 207
  • With some debugging, the bound value is a `StreamWrapper` of what I assume is the Java serialization of the `String[]` - `AC ED 00 05 75 74 00 ...` – OrangeDog Nov 15 '19 at 10:47
  • Further, `Arrays.asList(value)` binds as `varchar` for a single item, and `record` for multiple. – OrangeDog Nov 15 '19 at 11:07

1 Answers1

1

Instead of passing the String[] directly, wrap it in a TypedParameterValue.

The hibernate-types library does not yet support varchar[], but you can use it to build something that does:

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";
        }
    }
}
OrangeDog
  • 36,653
  • 12
  • 122
  • 207