array_contains generates varchar[] cast against a PostgreSQL text[] column — regression from 6.6
Environment
- Hibernate ORM: 7.4.1.Final (works on 6.6.49.Final)
- Database: PostgreSQL 17.x, driver
org.postgresql:postgresql - Dialect:
PostgreSQLDialect
Summary
When an entity maps a PostgreSQL text[] column to a String[] / List<String> attribute and a query uses the array_contains function, Hibernate 7.x renders the element argument with an explicit varchar[] cast:
... @> cast(array[?] as varchar array)
Because the physical column is text[], PostgreSQL rejects this — there is no implicit conversion between text[] and varchar[]:
ERROR: operator does not exist: text[] @> character varying[]
Hint: No operator matches the given name and argument types. You might need to add explicit type casts.
The identical mapping and query work on 6.6.49.Final; they fail on 7.4.1.Final.
Mapping
@Entity
@Table(name = "item")
public class Item {
@Id
@Column(name = "id")
private Long id;
// physical column: tags text[] NOT NULL
@Column(name = "tags", columnDefinition = "text[]")
private String[] tags;
}
DDL:
CREATE TABLE item (
id bigint PRIMARY KEY,
tags text[] NOT NULL
);
Query
em.createQuery(
"select count(i) from Item i where array_contains(i.tags, :tag)",
Long.class)
.setParameter("tag", "alpha")
.getSingleResult();
Generated SQL
7.4.1.Final (fails):
select count(i1_0.id)
from item i1_0
where i1_0.tags @> cast(array[?] as varchar array)
→ operator does not exist: text[] @> character varying[]
6.6.49.Final (works): the same query executes successfully — no cast to varchar[] (it targets a text-compatible type), so text[] @> text[] is used and PostgreSQL accepts it.
Workaround
I have, for now used the following custom function as a workaround :
public class TextArrayContainsFunction implements FunctionContributor {
@Override
public void contributeFunctions(FunctionContributions fc) {
fc.getFunctionRegistry().registerPattern(
"text_array_contains",
"(?1 @> array[?2]::text[])",
fc.getTypeConfiguration().getBasicTypeRegistry()
.resolve(StandardBasicTypes.BOOLEAN));
}
}