hypersistence-utils
hypersistence-utils copied to clipboard
isCollectionType returns false for ListArrayType
ListArrayType
is a grant child of AbstractStandardBasicType
which sets isCollectionType
to false
.
The impact is
When using CriteriaBuilder with the field it checks if attributeMetadata.isPlural()
by reading isCollectionType
.
The field should be ListAttribute
but since it is not a collection type it was builded as a SingularAttribute
.
And calling CriteriaBuilder.isMember() throws
unknown collection expression type [org.hibernate.query.criteria.internal.path.SingularAttributePath]
Code example:
A Entity
@TypeDef(name = "list-array", typeClass = ListArrayType.class)
public class AEntity implements Serializable {
ID and other fields..
@Type(type = "list-array")
@Column(name = "version", columnDefinition = "integer[]", nullable = false)
private List<Integer> version;
}
Queries
final CriteriaBuilder cb = entityManager.getCriteriaBuilder();
final CriteriaQuery<AEntity> cq = cb.createQuery(AEntity.class);
final Root<AEntity> root = cq.from(AEntity.class);
cq.select(root).where(cb.isMember(filter.getVersion(0), root.get("version")));
entityManager.createQuery(cq).getResultList();
Hibernate Types doesn't generate the Metamodel. Hibernate does that. So, this issue is a Hibernate problem.
Therefore, you need to open thr issue on the Hibernate project and provide a fix there.
ListArrayType should overwrite isCollectionType
to true. Or extend a different class like CollectionType
instead.
Send me a Pull Request with this fix plus a replicating test case and I'll check it out.
For anyone facing this, a temporary workaround
can be to integrated a direct DB function.
Example with above use case, on PostgreSQL:
cb.isNotNull(cb.function("array_position", Integer.class, filter.getVersion(0), root.get("version")))