Paul Taylor
Paul Taylor

Reputation: 13210

How do you put multiple indexes on a field using @index and hibernate

Given two fields in a table I can create an index that encompases both as follows:

@org.hibernate.annotations.Index(name = "IDX_REPORTID_RECNO")
private Integer reportId;

@org.hibernate.annotations.Index(name = "IDX_REPORTID_RECNO")
private Integer recNo;

and I can create indexes on each column individually

@org.hibernate.annotations.Index(name = "IDX_REPORTID")
private Integer reportId;

@org.hibernate.annotations.Index(name = "IDX_RECNO")
private Integer recNo;

But it doesnt let me do both, this doesn't seem to be allowed

@org.hibernate.annotations.Index(name = "IDX_REPORTID_RECNO")
@org.hibernate.annotations.Index(name = "IDX_REPORTID")
private Integer reportId;

@org.hibernate.annotations.Index(name = "IDX_REPORTID_RECNO")
@org.hibernate.annotations.Index(name = "IDX_RECNO")
private Integer recNo;

How can I do this ?

Upvotes: 3

Views: 1508

Answers (1)

axtavt
axtavt

Reputation: 242686

I think you can specify the second index at class level:

@org.hibernate.annotations.Table(indexes = 
    @org.hibernate.annotations.Index(name = "IDX_REPORTID_RECNO",
        columnNames = {"reportId", "recNo"})
)
public class MyEntity {
    @org.hibernate.annotations.Index(name = "IDX_REPORTID")
    private Integer reportId;

    @org.hibernate.annotations.Index(name = "IDX_RECNO")
    private Integer recNo;
}

Upvotes: 2

Related Questions