spring-data-jpa
spring-data-jpa copied to clipboard
Allow sort on JPA discriminator column or entity type [DATAJPA-1224]
Brice Roncace opened DATAJPA-1224 and commented
Using JPA 2's type operator, JPQL queries can filter or sort by entity type. However this is not possible using Spring Data JPA's Sort.
For example, given a simple class inheritance hierarchy:
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "type")
public abstract class Animal implements Serializable {...}
@Entity
public class Cat extends Animal {...}
@Entity
public class Bird extends Animal {...}
A full listing of animals sorted by type can be retrieved from the JpaRepository thusly:
public interface AnimalRepository extends JpaRepository<Animal,Long> {
@Query("from Animal a order by type(a)")
List<Animal> findAllSortedByType();
}
However this same type of query (where only an entity sort by type is desired) is not possible using a Sort parameter to findAll:
List<Animal> animals = animalRepository.findAll(new Sort("type"));
Affects: 1.11.8 (Ingalls SR8)
Reference URL: https://stackoverflow.com/q/43974210/225217