Closed agrancaric closed 2 months ago
Related to #3588.
We need to look up property paths to formulate a proper order expression. For findAll
we do not have any means to consider subclasses, we only have access to the repository's domain type.
We are exploring a switch from Criteria Queries towards String-based JPQL queries. In such an arrangement you could use JpaSort.unsafe(…)
to reference properties that would be resolved during query execution by your JPA provider.
Related to #3588.
We need to look up property paths to formulate a proper order expression. For
findAll
we do not have any means to consider subclasses, we only have access to the repository's domain type.We are exploring a switch from Criteria Queries towards String-based JPQL queries. In such an arrangement you could use
JpaSort.unsafe(…)
to reference properties that would be resolved during query execution by your JPA provider.
Thanks for the answer. With Hibernate (I am not sure about other providers) it is possible to get subclasses by using the following code (maybe there is also a a smarter way of doing this):
if (from.getModel() instanceof EntityDomainType<?> entityDomainType) {
return entityDomainType.getSubTypes().stream()
.map(Type::getJavaType)
.toList();
}
also there is a possibility of classpath scanning, not sure what do you think about that? We have a workaround implemented but if it is possible we would prefer to have it supported by Spring. If not then we'll just leave the workaround. Thanks in advance for your answer.
We do not want to change this behavior by default as subtypes open up exploitation vectors using sorting. If you want to use subtype properties for sorting, then use JpaSort.unsafe(…)
for these cases in the context of JPQL queries.
Hello, when trying to sort by subclass property using Spring Data Jpa repository an exception is thrown, however when using standard JPA the query executes fine. The problem seems to be in PropertyPath that only looks at the parent properties and throws an exception if the property is not found. For now I've added a workaround to also look at the subclasses in our project (https://github.com/croz-ltd/nrich/blob/dbd018ed73bc49f0f0bfd53c59435defcf1fed49/nrich-search/src/main/java/org/springframework/data/jpa/repository/query/NrichQueryUtils.java#L63) but I would prefer not to have to override and copy parts of QueryUtils. Is there an option to add such support in spring-data-jpa? The example project and exception stacktrace are given bellow.
spring-data-jpa-sorting-error.zip