Is it possible to specify via JPA or Hibernate annotations that a given index should use a range capable indexing method (such as a btree)? If so, how?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
While JPA allows you to model primary keys (@Id) and unique constraints (@UniqueConstraint), it does not allow you to explicitly model indexes. There is certainly no functionality in JPA standard to model/hint at index ranges or btrees, etc. JPA 2.0 Spec
Hibernate supports index modelling via @Index. Here’s an example:
@Entity
@org.hibernate.annotations.Table(name=”Forest”, indexes = { @Index(name=”idx”, columnNames = { “name”, “length” } ) } )
public class Forest { … }
Or for a single column index:
public class Trees {
}
It does not allow you to specify a “ranged-index”, btree, etc… Hibernate Annotations 3.5
IF hibernate generates the DB schema, then it will create indexes.
However, in any environment with reasonable QA applied, the DB schema is usually created and maintained separately by the DBA, not via hibernate automatic schema generation. This is much better practice, because it allows the DBA to define all of the physical attributes exactly as needed – just like the type of physical attribute you ask for in your Q ;-). Hibernate then works with the DBA’s strongly controlled version of the schema.
Whether or not the indexes are modelled by Hibernate/JPA, simply run a DB script, and an index is created. (Or you can apply text edits to the Hibernate/JPA generated script)
In the case of an Oracle DB, the default index type is already a B-Tree. In the case of MySQL the default index type is B-Tree, except in the case of in-memory storage engine, where default is HASH – but it is trivial to specify B-Tree in the index creation script. MySQL 5 – Create Index