How to create indexes using Hibernate for PostgreSQL

We are Borg

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

jrao77

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to create *_swap tables (w/ indexes) in postgresql

From Dev

Unable to create Postgresql table using Hibernate on Wildfly

From Dev

Create indexes for tables with prefix in PostgreSQL

From Dev

How to create Views using Hibernate?

From Dev

Using indexes in json array in PostgreSQL

From Dev

Using indexes in json array in PostgreSQL

From Dev

How to create indexes efficiently

From Dev

Create separate hibernate search indexes for each subclass

From Dev

Create separate hibernate search indexes for each subclass

From Dev

How to use gin indexes in postgresql

From Dev

create indexes for search using MongoTemplate?

From Dev

How to create tables in dynamoDB dynamically using Hibernate

From Dev

How to create an array of array indexes?

From Dev

Alphabetical sort in postgresql using indexes.

From Dev

Postgresql JPA Hibernate create Database

From Dev

how to create postgresql database using linux commandline

From Dev

How to create a polygon using fields in PostgreSQL?

From Dev

Postgresql: How do I ensure that indexes are in memory

From Dev

Postgresql 9.3: How to use crosstab with multiple indexes?

From Dev

Postgresql: How do I ensure that indexes are in memory

From Dev

How can I create a list that is created using the indexes and items of two other lists?

From Dev

How to create separate dataframe by looping over specific column indexes and using a conditional or statement in R?

From Dev

How to create separate dataframe by looping over specific column indexes and using a conditional or statement in R?

From Dev

Create PostgreSQL database on the fly using Hibernate even if the DB doesn't exist

From Dev

Create in WITH(CTE) using PostgreSQL

From Dev

How to create SessionFactory using Hibernate4 and Spring?

From Dev

How to create multiple indexes at once in RethinkDB?

From Dev

How to create indexes in MongoDB via .NET

From Dev

How to truncate column in order to create indexes?

Related Related

  1. 1

    How to create *_swap tables (w/ indexes) in postgresql

  2. 2

    Unable to create Postgresql table using Hibernate on Wildfly

  3. 3

    Create indexes for tables with prefix in PostgreSQL

  4. 4

    How to create Views using Hibernate?

  5. 5

    Using indexes in json array in PostgreSQL

  6. 6

    Using indexes in json array in PostgreSQL

  7. 7

    How to create indexes efficiently

  8. 8

    Create separate hibernate search indexes for each subclass

  9. 9

    Create separate hibernate search indexes for each subclass

  10. 10

    How to use gin indexes in postgresql

  11. 11

    create indexes for search using MongoTemplate?

  12. 12

    How to create tables in dynamoDB dynamically using Hibernate

  13. 13

    How to create an array of array indexes?

  14. 14

    Alphabetical sort in postgresql using indexes.

  15. 15

    Postgresql JPA Hibernate create Database

  16. 16

    how to create postgresql database using linux commandline

  17. 17

    How to create a polygon using fields in PostgreSQL?

  18. 18

    Postgresql: How do I ensure that indexes are in memory

  19. 19

    Postgresql 9.3: How to use crosstab with multiple indexes?

  20. 20

    Postgresql: How do I ensure that indexes are in memory

  21. 21

    How can I create a list that is created using the indexes and items of two other lists?

  22. 22

    How to create separate dataframe by looping over specific column indexes and using a conditional or statement in R?

  23. 23

    How to create separate dataframe by looping over specific column indexes and using a conditional or statement in R?

  24. 24

    Create PostgreSQL database on the fly using Hibernate even if the DB doesn't exist

  25. 25

    Create in WITH(CTE) using PostgreSQL

  26. 26

    How to create SessionFactory using Hibernate4 and Spring?

  27. 27

    How to create multiple indexes at once in RethinkDB?

  28. 28

    How to create indexes in MongoDB via .NET

  29. 29

    How to truncate column in order to create indexes?

HotTag

Archive