How to create indexes using Hibernate for PostgreSQL

I am working on a Spring-MVC application in which I use Hibernate as an ORM tool with PostgreSQL. For some objects in the project model, I would like to create indexes for faster searches. When I read, I found out that you can create indexes using Hibernate. Unfortunately, I was not very lucky. I was only trying to create it on one Model class, but when I check PGAdmin, I don't see any index for this table.

When I try to give the @Index parameter of the @Table annotation, I get an error. Can someone tell me how I can annotate columns and the whole table for automatic indexing by Hibernate. Many thanks.

User online model: // This class I 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;
}

, - :

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

, @Indexed .

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

, , . .: -)

+4
2

@Indexed Hibernate Search . .

, , ? ,

+2

@Index JPA :

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

, hbmddl. (, Flyway), .

( script), .

, Hibernate ( ), (, Flyway), script.

+3

All Articles