Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the @Index annotation work in JPA 2.1? [duplicate]

I'am trying to test JPA's @Index annotation which is avaliable since 2.1. But I can't find the documention how to use it.

When I'am trying:

@Index(columnList = "firstName")
private String firstName;

.. then Eclipse says: "The annotation @Index is disallowed for this location"

Does anyone know how to use it? Or has found the documentation for it? :-)

Thanks for any suggestions!

like image 616
Crayl Avatar asked Nov 02 '13 22:11

Crayl


People also ask

How index is defined in JPA?

JPA allows us to achieve that by defining indexes from our code using @Index. This annotation is interpreted by the schema generation process, creating artifacts automatically. Note that it's not necessary to specify any index for our entities.

What is @entity annotation in spring boot?

The @Entity annotation specifies that the class is an entity and is mapped to a database table. The @Table annotation specifies the name of the database table to be used for mapping.

What is @basic annotation in hibernate?

We can use the @Basic annotation to mark a basic type property: @Entity public class Course { @Basic @Id private int id; @Basic private String name; ... } In other words, the @Basic annotation on a field or a property signifies that it's a basic type and Hibernate should use the standard mapping for its persistence.

What is EntityManager in Spring data JPA?

The EntityManager API is used to create and remove persistent entity instances, to find entities by their primary key, and to query over entities. The set of entities that can be managed by a given EntityManager instance is defined by a persistence unit.


1 Answers

I've used the hibernate @Index annotation just as you have with no issues, but it appears that the JPA @Index annotation is stricter on where it can be used. Taken from a previous question - The annotation @Index is disallowed for this location :

The JPA Index annotation can only be used as part of another annotation like @Table, @SecondaryTable, etc.

@Table(indexes = { @Index(...) }) 
like image 182
John Farrelly Avatar answered Sep 21 '22 05:09

John Farrelly