vladmihalcea/hypersistence-utils

StringArrayType.INSTANCE is not working with a two dimensional String array parameter on PostgreSQL

Open

#436 opened on May 10, 2022

View on GitHub
 (13 comments) (1 reaction) (0 assignees)Java (354 forks)batch import
good first issuerequires test case

Repository metrics

Stars
 (2,184 stars)
PR merge metrics
 (Avg merge 9d 17h) (1 merged PR in 30d)

Description

Hibernate ORM core version: 5.6.8.Final Hibernate Types 55 version: 2.16.2

StringArrayType is expected to work with multidimensional arrays based on the Javadoc, but that isn't the case when used with Hibernate native query parameters and PostgreSQL.

One dimensional array parameter is working as expected:

public Customer findWithOneDimensionalArrayParameter() {
	return (Customer) entityManager
			.createNativeQuery(
					"SELECT 1 AS id, 'foo' AS first_name, 'bar' AS last_name WHERE :param = ARRAY['foo'];",
					Customer.class
			)
			.unwrap(Query.class)
			.setParameter("param", new String[] {"foo"}, StringArrayType.INSTANCE)
			.getSingleResult();
}

But two dimensional array parameter isn't working:

public Customer findWithTwoDimensionalArrayParameter() {
	return (Customer) entityManager
			.createNativeQuery(
					"SELECT 1 AS id, 'foo' AS first_name, 'bar' AS last_name WHERE :param = ARRAY[ARRAY['foo'], ARRAY['bar']];",
					Customer.class
			)
			.unwrap(Query.class)
			.setParameter("param", new String[][] {new String[] {"foo"}, new String[] {"bar"}}, StringArrayType.INSTANCE)
			.getSingleResult();
}

The Exception:

java.lang.IllegalArgumentException: Array-valued parameter value element [[Ljava.lang.String;@f10d055] did not match expected type [[Ljava.lang.String; (n/a)]

Demo: demo.zip

Thanks having a look.

Contributor guide