David L
David L

Reputation: 44657

How to specify a range index in hibernate annotations

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?

Upvotes: 0

Views: 944

Answers (1)

Glen Best
Glen Best

Reputation: 23105

  • 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 {

      @Index(name="story1index")
      public String getLeaf() {
         return leaf;
      }
    

    }

    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

Upvotes: 3

Related Questions