Spring-Data Pageable sort on JPA discriminator field

Viewed 1808

I've got a JPA entity hierarchy with JPA joined inheritance:

@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "TYPE", discriminatorType = DiscriminatorType.STRING)
Product

and

@Entity
@Table(name = "product_quiz")
@DiscriminatorValue("quiz")
public class QuizProduct extends Product

I want to run a spring-data query with QueryByExample and PageRequest like this:

Example<Product> example = ...
PageRequest pageRequest = ...
Page<Product> productPage = productRepository.findAll(example, pageRequest);

It works fine, except if I set sortField of the pageRequest to be "type" (the @DiscriminatorColumn).

I've tried to:

  • directly add the type field to the entity. This way the query runs fine, but the persisting operation fails with org.postgresql.util.PSQLException: The column index is out of range: 11, number of columns: 10.
  • if I mark this field as @Transient, the persisting works, but the query gives me this exception: Unable to locate Attribute with the the given name [type] on this ManagedType [...AbstractEntityWithDate] (AbstractEntityWithDate is a @MappedSuperclass of Product, which contains creationDate and modificationDate)

I'm using spring-boot version 1.4.5.RELEASE with the dependency-managed versions of spring-data-jpa(1.10.8.RELEASE) and hibernate(5.0.12.Final).

How can I configure the sorting by discriminator field with Pageable? (I don't want to write custom @Query for this).

1 Answers
Related