【问题标题】:How to enforce JPA to use composite primary key index order如何强制 JPA 使用复合主键索引顺序
【发布时间】:2020-11-24 00:03:42
【问题描述】:

表名:转移

列:IDENTIFIER、KEY、TRANSFER_NUMBER、FIRST_NAME、LAST_NAME

Transfer的表复合主键为:IDENTIFIER,KEY,TRANSFER_NUMBER

Transfer的表索引INDEX1定义:IDENTIFIER ASC, KEY ASC, TRANSFER_NUMBER ASC

转移pojo

@Entity
@Table(name = "TRANSFER", indexes = { @Index(name = "INDEX1", columnList = "identifier,key,transferNbr") })
@Getter
@Setter
@DynamicUpdate
public class Transfer implements Serializable, Cloneable {

    private static final long serialVersionUID = 1L;

    @EmbeddedId
    private TransferPk id;
    
    @Column(name = "FIRST_NAME")
    private String firstName;
    
    @Column(name = "LAST_NAME")
    private String lastName;

TransferPk pojo

        @EqualsAndHashCode
        @Getter
        @Setter
        @Embeddable
        public class TransferPk implements Serializable {

        private static final long serialVersionUID = 1L;

        @Column(name = "IDENTIFIER")
        private String identifier;
        @Column(name = "KEY")
        private int key;
        @Column(name = "TRANSFER_NUMBER")
        private String transferNbr;

检索转移的代码

    TransferPk id = new TransferPk();
    id.setIdentifier("123");
    id.setKey(456);
    id.setTransferNbr("789");
    // This JPA query also uses the reverse Transfer of index
    Optional<Transfer> Transfer = TransferRepo.findById(id);

我能够毫无问题地从数据库获取数据,并且 HQL 是

    select <all fileds> from TRANSFER transfer0_ where transfer0_.TRANSFER_NUMBER=? and transfer0_.KEY=? and transfer0_.IDENTIFIER=?

虽然我们使用了索引注释,但 JPA 仍然没有使用传输表 (INDEX1) 中定义的索引,因此检索需要更多时间。

如何强制 JPA 使用索引顺序。非常感谢任何帮助。谢谢

【问题讨论】:

    标签: java hibernate jpa orm


    【解决方案1】:

    我不知道您所说的“索引顺序”是什么意思,但您在 @Index 注释中使用了 JPA 属性名称,这是错误的。使用像@Index(name = "INDEX1", columnList = "identifier,key,transfer_number")这样的列名

    【讨论】:

    • 谢谢克里斯蒂安。我已经尝试使用属性名称,下面是错误。 Invocation of init method failed; nested exception is org.hibernate.AnnotationException: Unable to create index (identifier, key, transferNbr) on table TRANSFER: database column 'identifier', 'key', 'transferNbr' not found. Make sure that you use the correct column name which depends on the naming strategy in use (it may not be the same as the property name in the entity, especially for relational types)
    • 我告诉过你使用列名 :D 使用属性名是错误的
    • 奥普斯。行。尝试过的列名及以上是结果。谢谢
    【解决方案2】:

    您创建索引的所有方式都是错误的。列列表将始终具有表列名称而不是字段名称。

    在你的情况下是:---

    @Table(name = "TRANSFER", indexes = { @Index(name = "INDEX1", columnList = "IDENTIFIER,KEY,TRANSFER_NUMBER ASC") })
    

    “DESC/ASC”用于索引顺序。

    希望,它应该可以解决您的问题。

    【讨论】:

    • 感谢 privranjan。尝试了 ASC 和 DESC,但仍然没有选择正确的索引顺序 执行 HQL select &lt;all fileds&gt; from TRANSFER transfer0_ where transfer0_.TRANSFER_NUMBER=? and transfer0_.KEY=? and transfer0_.IDENTIFIER=? 预期 HQL 应该是 select &lt;all fileds&gt; from TRANSFER transfer0_ where transfer0_.IDENTIFIER=? and transfer0_.KEY=? and transfer0_.TRANSFER_NUMBER=?
    • 我们在表中有大约 1B 个数据,DB2 中的索引顺序是IDENTIFIER,KEY,TRANSFER_NUMBER。但 JPA 使用订单为TRANSFER_NUMBER,IDENTIFIER,KEY。所以我们最终会遇到性能问题。为避免,我试图强制 JPA 使用 DB2 中定义的相同顺序
    • 你使用的是哪个休眠版本?
    • 检查休眠 4.3.8 或 5.2.13。它不适用于 5.2.10 版本
    • 我的版本是5.3.7
    猜你喜欢
    • 2011-12-29
    • 1970-01-01
    • 2018-08-30
    • 1970-01-01
    • 1970-01-01
    • 2012-01-29
    • 1970-01-01
    • 2011-09-21
    • 1970-01-01
    相关资源
    最近更新 更多