gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 00:15:41 24 4
gpt4 key购买 nike

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

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


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

我想使用 QueryByExample 和 PageRequest 运行 Spring 数据查询像这样:
Example<Product> example = ...
PageRequest pageRequest = ...
Page<Product> productPage = productRepository.findAll(example, pageRequest);

它工作正常,除非我设置 sortFieldpageRequest成为“类型”( @DiscriminatorColumn )。

我试过:
  • 直接在实体中添加类型字段。这样查询运行良好,但持久化操作失败 org.postgresql.util.PSQLException: The column index is out of range: 11, number of columns: 10 .
  • 如果我将此字段标记为 @Transient ,持久的工作,但查询给了我这个异常(exception):Unable to locate Attribute with the the given name [type] on this ManagedType [...AbstractEntityWithDate] (AbstractEntityWithDate 是 @MappedSuperclassProduct ,其中包含 creationDatemodificationDate )

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

    如何使用 Pageable 按鉴别器字段配置排序? (我不想为此编写自定义 @Query)。

    最佳答案

    Discriminator columns can be mapped as read-only entity attributes


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

    然后,以下查询将起作用:
    productRepository.findAll(new Sort("type"))

    提供工作 sample on Github .

    话虽如此,试图在具有继承策略的代码中使用鉴别器值 JOINED不适用于 Hibernate( type 将始终为 null )。这是因为传统上 Hibernate 不支持 @DiscriminatorColumnJOINED (请参阅 HHH-6911 )并且仅从 v4.2.9 开始保存联接继承的列值。即使正确保存了该值,似乎仍然存在不读取鉴别器列值的错误。

    关于hibernate - JPA 鉴别器字段上的 Spring-Data Pageable 排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43974210/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com