Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

I am working on a Spring-MVC application in which I am using Hibernate as the ORM tool with PostgreSQL. For some of the entities in the project Model, I would like to create Indexes for faster lookup. As I read, I found out it is possible to create indexes using Hibernate. Unfortunately, I am not having much luck. I only tried to create it on one Model class, but when I check in PGAdmin, I cannot see any index for that table.

When I try giving the @Index parameter to the @Table annotation, I get error. Can anyone tell me how I can annotate columns and entire table for auto-indexing by Hibernate. Thanks a lot.

Online User model : // This class I just used for testing

import org.hibernate.search.annotations.Indexed;

import javax.persistence.*;
@Entity
@Table(name="onlineusers" )
@Indexed(index = "onlineuserindex")
public class OnlineUsers {

  @Id
    @Column(name="onlineuserid")
    @GeneratedValue(strategy = GenerationType.SEQUENCE,generator = "online_gen")
    @SequenceGenerator(name = "online_gen",sequenceName = "online_seq")
    private int onlineUserId;


    @Column(name = "onlineusername")
    private String personName;
}

Please note, when I try something like this below :

@Indexed(index = "usernameindex");
@Column(name="username");
private String userName;

I get an error, @Indexed not applicatble to a field.

POM.xml :

 <properties>
        <java-version>1.8</java-version>
        <org.springframework-version>4.0.6.RELEASE </org.springframework-version>
        <org.aspectj-version>1.7.4</org.aspectj-version>
        <org.slf4j-version>1.7.5</org.slf4j-version>
        <hibernate.version>4.3.9.Final</hibernate.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>

  <!-- Hibernate search dependencies -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-search-orm</artifactId>
            <version>5.2.0.Final</version>
        </dependency>

    <!--    <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-annotations</artifactId>
            <version>3.5.6-Final</version>
        </dependency>
-->

Kindly let me know what I am doing wrong. Thanks a lot. :-)

share|improve this question

2 Answers 2

up vote 2 down vote accepted

The @Indexed annotation from Hibernate Search is only applicable to types. So you cannot use those on attributes.

Reading your question, it seems that you want to add a database index to the table? if so then you have to use Index Annotation

share|improve this answer
    
Thank you for your answer, Unfortunately whenever I search on Google, I am getting result back relevant to Hibernate-search. You documentation points me to Javadocs, and nothing specific in it, which makes it rather difficult to find. Can you please give some example or the actual API/method I would require. –  We are Borg May 11 at 9:57
    
oops my mistake, have corrected the link now –  jrao77 May 11 at 10:00
    
I came up to the same link, to be honest, I really dont know what to do. –  We are Borg May 11 at 10:02
    
The @Index column is used to annotate a field which you want the index to be created. So if you want a database index to be created on the field username then you have to annotate the field. Optionally you can specify a name. Here is an example from Hibernate docs - docs.jboss.org/ejb3/app-server/HibernateAnnotations/reference/… –  jrao77 May 11 at 10:08
    
Multiple columns can also be used to create the index(usually at the type level) with the @Table annotation. For ex: @org.hibernate.annotations.Table( indexes = {@Index(name = "idx", columnNames = {"id_1", "Id2"}}) At the field level you can use something like this - @Index(name = "idx_1") private User user1; –  jrao77 May 11 at 10:16

You can use the @Index JPA annotation:

@Entity
@Table(name = "onlineusers",
    indexes = {
        @Index(name = "usernameindex",  columnList="username", unique = true)   
    }
)
public class OnlineUsers {
   ...
}

This is only applied if you use the automatic hbmddl schema generation. If the database schema is generated out of Hibernate, like when using Flyway, then these annotation won't have any effect.

When you delegate the database schema generation to an external process (a database migration tool or a manual script update procedure), then you need to include the indexes in the migration scripts as Hibernate cannot update an externally generated schema, by figuring out what's are the differences.

You either generate the whole schema with Hibernate (which is not even recommended for production systems) or you rely on a database migration framework (e.g. Flyway) and the indexes are simply included in an incremental schema update script.

share|improve this answer
    
Thanks, but why am I not able to see the indexes created in PGadmin? But when I use @Index from Hibernate on field, I can see them in Pgadmin. –  We are Borg May 11 at 12:49
    
I assume you generate your database schema with hbmddl, right? –  Vlad Mihalcea May 11 at 12:50
    
No, By myself. I wrote the database table code myself and executed it in SQL console. –  We are Borg May 11 at 12:51
    
Check my updated answer. –  Vlad Mihalcea May 11 at 13:05
    
I think I will stick to field Indexes which are atleast getting generated. Thanks for your help. –  We are Borg May 11 at 13:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.