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. :-)
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