5

The hibernate annotation @Index is deprecated, but I can't find docs or any hints how to exchange it.

@Entity
class MyEntity {
      @Index(name = "name") //org.hibernate.annotations.Index
      private String name;

      @Index(name = "age")
      private int age;
}

Result: @deprecated Using {@link javax.persistence.Index} instead. But if I change the import: Annotation disallowed for this location.

So: how does the above entity have to look like when using non-deprecated index?

membersound
  • 81,582
  • 193
  • 585
  • 1,120

1 Answers1

4

As specified by the answer here the index annotation in javax.persistence.Index can only be used as part of another annotation.

@Table(name = "myTable", indexes = { @Index("name")})
@Entity
class MyEntity {
      @Index(name = "name")
      private String name;

      @Index(name = "age")
      private int age;
}
Community
  • 1
  • 1
David
  • 19,577
  • 28
  • 108
  • 128
  • Should I only add the "name" index withint the `@Table` annotation? Or should I list all annotated indexes there? – membersound Apr 01 '14 at 08:28
  • If you just want "name" then just use name, it depends how many indexes you want on your table. Don't forget you can have index combinations. – David Apr 01 '14 at 08:55
  • Could you correct your code example or make it more clear? If I understood correctly, the properties should not have any `@Index` annotations? Or is the annotation needed both in `@Table` and the property? Ist it possible to give indices a name? Hibernate's `@Index` supports this. – Jack Nov 12 '16 at 18:26
  • 1
    A more complete code exmple can be found in borjab's answer to the following question: http://stackoverflow.com/q/3405229/1492329 – Jack Nov 13 '16 at 10:36