【问题标题】:Hibernate index with DESC order具有 DESC 顺序的休眠索引
【发布时间】:2016-12-28 19:06:43
【问题描述】:

有没有办法用 DESC 顺序的一列创建多列索引?我试过这个(来自 javax.persistence 包的注释):

@Table(name = some_table
  indexes = {
    @Index(name = "idx_multi_column", columnList = "column1, column2, column3"),
    @Index(name = "idx_multi_column", columnList = "column1, column2, column3 DESC")
  }
)

但它使用 column3 asc 创建两个索引 - DESC 被忽略。我正在使用:

休眠 4.3.10 休眠 JPA 2.1 api

谢谢。

【问题讨论】:

  • 我很好奇,你找到解决方案了吗?
  • 不,我也无法破解它。

标签: java hibernate jpa indexing jpa-2.1


【解决方案1】:

根据定义-即the JPA specification¹文档又名JSR 338-有一种方法可以注释所需的行为,就像您在问题中描述的那样。引用规范,第 11.1.23 节索引注释,第 452 页:


@Index 注释用于模式生成。 [..] 索引注释 可用于指定主键索引中列的顺序。

@Target({}) @Retention(RUNTIME)
public @interface Index {
 String name() default "";
 String columnList();
 boolean unique() default false;
}

columnList元素的语法是column_list,如下:

column::= index_column [,index_column]*
index_column::= column_name [ASC | DESC]

持久性提供程序必须遵守指定的列顺序。

如果未指定 ASCDESC,则假定为 ASC(升序)。


结论

任何持久性提供者都必须(应该)遵守此要求以符合 JPA 规范。因此,我可以想象 4.3.10 版本中的 Hibernate 要么 (a) 遇到错误,要么 (b) 不遵守该特定点的规范。

想法

  • 使用更新的版本重试,例如 Hibernate 5+,例如current release 5.4.21
  • 尝试使用其他 JPA 提供商,例如 EclipseLinkOpenJPA
  • 检查您编译的类是否使用正确的语法运行/测试。也许,它们可能已经使用过时的版本运行/测试过?

希望对你有帮助。

脚注

¹ 在 2.2 版或之前的 2.1/2.0 版中

【讨论】:

猜你喜欢
  • 2020-08-23
  • 1970-01-01
  • 2014-04-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-14
相关资源
最近更新 更多