【问题标题】:Eclipselink, index on enumerated collection?Eclipselink,枚举集合的索引?
【发布时间】:2015-06-01 19:08:22
【问题描述】:

我有一个带有枚举集合的实体 (FitableEntity),我想在枚举字符串上设置一个索引。这没有效果(没有错误,没有索引):

@Enumerated(EnumType.STRING)
@ElementCollection
@Updateable
@Index(table="fitableentity_state", columnNames={"state"})
private Set<FitableEntityState> state = numSet.of(FitableEntityState.Inactive);

我没有像我期望的那样在fitableentity_state 表中获得index 的状态。

有没有办法通过注释来做到这一点,还是迁移是唯一的选择?

谢谢。

【问题讨论】:

  • 你需要一个收藏表:stackoverflow.com/a/16014318/351861
  • 集合表正确生成没有 CollectionTable(表 fitableentity_state 列 fitableentity_id (fk) 和 state)。添加 CollectionTable 没有任何效果。
  • 您是否尝试过使用 CollectionTable 的索引字段?它应该这样工作。
  • 这不是 JPA 的工作方式 - JPA 永远不会在没有额外配置的情况下自行创建表。如果您的集合是空的,JPA 根本无法映射任何有效的行集,因为您的表与实体和实体关系不匹配,或者没有数据 - 其他一切都会引发错误。大多数情况下,您的声明与现有数据库不匹配;这有时会导致看似空的集合,而数据库本身确实有行
  • @specialzt 不知道该告诉您什么,使用 Eclipselink 2.4,您无需使用 CollectionTable 来获取 ElementCollection 以映射到辅助表。没有错误,一切都像宣传的那样工作,只是无法让索引注释做我想做的事。

标签: jpa eclipselink


【解决方案1】:

为了将来参考,这是一个解决方案:

   @CollectionTable(indexes={@Index(columnList="state")})
   @Enumerated(EnumType.STRING)
   @ElementCollection(targetClass = FitableEntityState.class)
   @Updateable
   private Set<FitableEntityState> state = EnumSet.of(FitableEntityState.Inactive);

ElementCollection 可以重构为CollectionTable,但是对于 Eclipselink 2.6,这可以很好地与

<property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
<property name="eclipselink.ddl-generation.output-mode" value="database" />

在persistence.xml中

【讨论】:

    猜你喜欢
    • 2013-01-29
    • 1970-01-01
    • 1970-01-01
    • 2020-08-10
    • 2015-10-14
    • 2013-01-25
    • 2011-04-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多