admin管理员组文章数量:1123505
i am currently working on a quarkus project which utilizes hibernate and a criteriabuilder to access data on postrgresql databases.
We recently updated the Quarkus BOM, which included upgrading Hibernate (core) from version 6.2 to 6.6. After upgrading, we now receive an error when building a postgresql command using the jakarta.persistence.criteria.CriteriaBuilder:
Parameter 0 of function 'array_position()' requires an array type, but argument is of type 'java.util.List'
This specific error is thrown at this sub-function on the last line:
/**
* Creates an expression that can be used to determine if the array at the given path contains the given element.
*
* @param criteriaBuilder The criteria builder.
* @param arrayPath The path to the array.
* @param element The element to look for.
* @param <T> The type of the elements in the array. The element must have the same type.
* @return The expression to check an array for the given element.
*/
public <T> Predicate createArrayContainsExpression(final CriteriaBuilder criteriaBuilder,
final Path<String> arrayPath,
final Expression<T> element) {
// array_position($array, $element) returns the index of $element in $array or NULL, if it is not contained.
// If looking for null, we pass null for $element, the result will be non-null (an integer),
// if the array contains null or null if the array does not contain null. That's why we check the result with
// IS NOT NULL. This is more intuitive for non-null values of element.
// The following code translates to the SQL:
// array_position(array, $element) IS NOT NULL
return criteriaBuilder.isNotNull(criteriaBuilder.function(
"array_position",
Boolean.class,
arrayPath,
element
));
}
The input arrayPath
of createArrayContainsExpression
points to a field of a entity class, which seems to be the issue. Here is a minimal example:
@Column(name = "demand_types")
@JdbcTypeCode(Types.ARRAY)
@Convert(converter = DemandType.DemandTypeListConverter.class)
private List<DemandType> demandTypes;
The DemandTypeListConverter
implements AttributeConverter
(see below) and is responsible for converting between a short value within the database and enums in java (DemandType
is an enum).
public interface DbEnum {
Short getDbValue();
static <T extends DbEnum> T convert(Class<T> enumClass, Short value) {
Short nonNullValue = value == null ? -1 : value;
return Stream.of(enumClass.getEnumConstants())
.filter(c -> c.getDbValue().equals(nonNullValue))
.findFirst()
.orElseThrow(IllegalArgumentException::new);
}
abstract class EnumConverter<T extends DbEnum> implements AttributeConverter<T, Short> {
/**
* Maps enum values to database values of type short/smallint
*
* @param dbEnum the enum value that should be mapped to a database entry
* @return the integer value corresponding to the enum value.
*/
@Override
public Short convertToDatabaseColumn(T dbEnum) {
if (dbEnum == null) {
return null;
}
return dbEnum.getDbValue();
}
}
/**
* Base class for converters between list typed {@link DbEnum} entity attributes and smallint / short typed database
* array attributes.
*
* @param <T> The concrete type of {@link DbEnum}.
*/
abstract class EnumListConverter<T extends DbEnum> implements AttributeConverter<List<T>, Short[]> {
@Override
public final Short[] convertToDatabaseColumn(final List<T> attribute) {
if (attribute == null) {
return null;
}
Short[] dbValues = new Short[attribute.size()];
for (int i = 0; i < attribute.size(); i++) {
dbValues[i] = attribute.get(i).getDbValue();
}
return dbValues;
}
@Override
public final List<T> convertToEntityAttribute(final Short[] dbData) {
if (dbData == null) {
return null;
}
List<T> dbEnumList = new ArrayList<>();
for (Short dbValue : dbData) {
final T converted = convert(dbValue);
dbEnumList.add(converted);
}
return dbEnumList;
}
public abstract T convert(final Short dbValue);
}
}
What have I tried already?
- Using a older quarkus-bom version which includes older hibernate versions. Did not work, as older quarkus-bom versions lead to the same issue. Also, older quarkus-bom versions include a CVE which I was trying to remove in the first place.
- Use Java arrays instead of Lists in my entity-class. This resolved to a similiar issue:
Parameter 0 of function 'array_position()' requires an array type, but argument is of type 'com.bmw.ispi.leads.cockpit.lead.handling.entity.enums.NotificationStatus[]'
- Tried to utilize a GitHub issue which seemed to have a similiar topic. However, I did not completely match my issue, as I found out after further investigation.
Does anyone have further ideas what the issue might be and how I can resolve it?
i am currently working on a quarkus project which utilizes hibernate and a criteriabuilder to access data on postrgresql databases.
We recently updated the Quarkus BOM, which included upgrading Hibernate (core) from version 6.2 to 6.6. After upgrading, we now receive an error when building a postgresql command using the jakarta.persistence.criteria.CriteriaBuilder:
Parameter 0 of function 'array_position()' requires an array type, but argument is of type 'java.util.List'
This specific error is thrown at this sub-function on the last line:
/**
* Creates an expression that can be used to determine if the array at the given path contains the given element.
*
* @param criteriaBuilder The criteria builder.
* @param arrayPath The path to the array.
* @param element The element to look for.
* @param <T> The type of the elements in the array. The element must have the same type.
* @return The expression to check an array for the given element.
*/
public <T> Predicate createArrayContainsExpression(final CriteriaBuilder criteriaBuilder,
final Path<String> arrayPath,
final Expression<T> element) {
// array_position($array, $element) returns the index of $element in $array or NULL, if it is not contained.
// If looking for null, we pass null for $element, the result will be non-null (an integer),
// if the array contains null or null if the array does not contain null. That's why we check the result with
// IS NOT NULL. This is more intuitive for non-null values of element.
// The following code translates to the SQL:
// array_position(array, $element) IS NOT NULL
return criteriaBuilder.isNotNull(criteriaBuilder.function(
"array_position",
Boolean.class,
arrayPath,
element
));
}
The input arrayPath
of createArrayContainsExpression
points to a field of a entity class, which seems to be the issue. Here is a minimal example:
@Column(name = "demand_types")
@JdbcTypeCode(Types.ARRAY)
@Convert(converter = DemandType.DemandTypeListConverter.class)
private List<DemandType> demandTypes;
The DemandTypeListConverter
implements AttributeConverter
(see below) and is responsible for converting between a short value within the database and enums in java (DemandType
is an enum).
public interface DbEnum {
Short getDbValue();
static <T extends DbEnum> T convert(Class<T> enumClass, Short value) {
Short nonNullValue = value == null ? -1 : value;
return Stream.of(enumClass.getEnumConstants())
.filter(c -> c.getDbValue().equals(nonNullValue))
.findFirst()
.orElseThrow(IllegalArgumentException::new);
}
abstract class EnumConverter<T extends DbEnum> implements AttributeConverter<T, Short> {
/**
* Maps enum values to database values of type short/smallint
*
* @param dbEnum the enum value that should be mapped to a database entry
* @return the integer value corresponding to the enum value.
*/
@Override
public Short convertToDatabaseColumn(T dbEnum) {
if (dbEnum == null) {
return null;
}
return dbEnum.getDbValue();
}
}
/**
* Base class for converters between list typed {@link DbEnum} entity attributes and smallint / short typed database
* array attributes.
*
* @param <T> The concrete type of {@link DbEnum}.
*/
abstract class EnumListConverter<T extends DbEnum> implements AttributeConverter<List<T>, Short[]> {
@Override
public final Short[] convertToDatabaseColumn(final List<T> attribute) {
if (attribute == null) {
return null;
}
Short[] dbValues = new Short[attribute.size()];
for (int i = 0; i < attribute.size(); i++) {
dbValues[i] = attribute.get(i).getDbValue();
}
return dbValues;
}
@Override
public final List<T> convertToEntityAttribute(final Short[] dbData) {
if (dbData == null) {
return null;
}
List<T> dbEnumList = new ArrayList<>();
for (Short dbValue : dbData) {
final T converted = convert(dbValue);
dbEnumList.add(converted);
}
return dbEnumList;
}
public abstract T convert(final Short dbValue);
}
}
What have I tried already?
- Using a older quarkus-bom version which includes older hibernate versions. Did not work, as older quarkus-bom versions lead to the same issue. Also, older quarkus-bom versions include a CVE which I was trying to remove in the first place.
- Use Java arrays instead of Lists in my entity-class. This resolved to a similiar issue:
Parameter 0 of function 'array_position()' requires an array type, but argument is of type 'com.bmw.ispi.leads.cockpit.lead.handling.entity.enums.NotificationStatus[]'
- Tried to utilize a GitHub issue https://github.com/vladmihalcea/hypersistence-utils/issues/708 which seemed to have a similiar topic. However, I did not completely match my issue, as I found out after further investigation.
Does anyone have further ideas what the issue might be and how I can resolve it?
Share Improve this question asked 17 hours ago Fabian SieperFabian Sieper 111 silver badge1 bronze badge New contributor Fabian Sieper is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.1 Answer
Reset to default 1As of Hibernate ORM 6.6 array functions are supported out of the box, but your entity mapping seems a bit odd.
Please remove the @Convert(converter = DemandType.DemandTypeListConverter.class)
part from the mapping. Hibernate ORM is able to map the list to a native array automatically.
Also, Hibernate ORM has a array_contains
function that you can use, or in your case array_contains_nullable
:
public <T> Predicate createArrayContainsExpression(final CriteriaBuilder criteriaBuilder,
final Path<?> arrayPath,
final Expression<T> element) {
return criteriaBuilder.isTrue(criteriaBuilder.function(
"array_contains_nullable",
Boolean.class,
arrayPath,
element
));
}
本文标签:
版权声明:本文标题:postgresql - Parameter 0 of function 'array_position()' requires an array type, but argument is of type ' 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736572832a1944792.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论