JPA鉴别器字段上的Spring-Data Pageable排序

C-S*_*ark 4 hibernate jpa spring-data spring-data-jpa

我有一个JPA实体层次结构,JPA加入了继承:

@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "TYPE", discriminatorType = DiscriminatorType.STRING)
Product
Run Code Online (Sandbox Code Playgroud)

和

@Entity
@Table(name = "product_quiz")
@DiscriminatorValue("quiz")
public class QuizProduct extends Product
Run Code Online (Sandbox Code Playgroud)

我想用QueryByExample运行一个spring-data查询,PageRequest如下所示:

Example<Product> example = ...
PageRequest pageRequest = ...
Page<Product> productPage = productRepository.findAll(example, pageRequest);
Run Code Online (Sandbox Code Playgroud)

它工作正常,除非我设置sortField为pageRequest"类型"(the @DiscriminatorColumn).

我试过:

  • 直接将类型字段添加到实体.这样查询运行正常,但持久化操作失败org.postgresql.util.PSQLException: The column index is out of range: 11, number of columns: 10.
  • 如果我纪念这一领域的@Transient,持续存在的作品,但查询给了我这样的例外:Unable to locate Attribute with the the given name [type] on this ManagedType [...AbstractEntityWithDate](AbstractEntityWithDate是@MappedSuperclass的Product,它包含creationDate和modificationDate)

我使用spring-boot版本1.4.5.RELEASE与依赖管理版本的spring-data-jpa(1.10.8.RELEASE)和hibernate(5.0.12.Final).

如何通过鉴别器字段配置排序Pageable?(我不想为此写自定义@Query).

man*_*ish 6

Discriminator列可以映射为只读实体属性

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "TYPE", discriminatorType = DiscriminatorType.STRING)
class Product {
  @Column(name = "TYPE", insertable = false, updatable = false)
  private String type;
}
Run Code Online (Sandbox Code Playgroud)

然后,以下查询将起作用:

productRepository.findAll(new Sort("type"))
Run Code Online (Sandbox Code Playgroud)

Github上提供的工作示例.


说到这一点,试图在代码中使用继承策略中的鉴别器值JOINED将不适用于Hibernate(type将永远是null).这是因为传统的Hibernate已经不支持@DiscriminatorColumn使用JOINED(见HHH-6911),并已开始仅保存与v4.2.9起连接继承列值.似乎仍有一个错误不会读取鉴别器列值,即使该值已正确保存.