0

I have an entity (FitableEntity) with an enumerated collection and I'd like to set an index on the enumeration string. This has no effect (no error, no index):

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

I don't get an index on state in the table fitableentity_state as I'd expect.

Is there a way to do this via annotation or is a migration the only choice?

Thanks.

Mike Summers
  • 2,139
  • 3
  • 27
  • 57
  • You need a collectiontable for collections : http://stackoverflow.com/a/16014318/351861 – specializt Jun 01 '15 at 19:26
  • The collection table is correctly generated _without_ CollectionTable (table fitableentity_state with columns fitableentity_id (fk) and state). Adding CollectionTable does not have any effect. – Mike Summers Jun 01 '15 at 20:29
  • 1
    Have you tried using the indexes field of CollectionTable? It should work that way. – Petros Splinakis Jun 02 '15 at 08:47
  • thats not how JPA works - JPA will never create tables on its own without additional configuration. If your collection is empty JPA simply cant map any valid rowsets because either your tables dont match the entities and entity-relationships or there is no data - everything else will throw errors. Most of the time your declarations dont match the existing database; which sometimes will result in seemingly empty collections whereas the database itself indeed has rows – specializt Jun 02 '15 at 08:47
  • @specialzt don't know what to tell you, with Eclipselink 2.4 you don't need to use CollectionTable to get get a ElementCollection to map to a secondary table. No errors, everything works as advertised, just can't get the Index annotation to do what I want. – Mike Summers Jun 02 '15 at 12:16
  • @PetrosSplinakis, thanks, I was looking at an old version of the Java doc, I'll give that a go. – Mike Summers Jun 02 '15 at 12:19

1 Answers1

0

For future reference this is a solution:

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

ElementCollection can be refactored out into CollectionTable but for Eclipselink 2.6 this works nicely with

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

in persistence.xml

Mike Summers
  • 2,139
  • 3
  • 27
  • 57