如何使用@index和hibernate在字段上放置多个索引

时间:2013-01-18 08:50:13

标签: java hibernate annotations indexing

给定表中的两个字段,我可以创建一个包含如下两个字段的索引:

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

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

我可以在每个列上单独创建索引

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

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

但它不允许我这两个,这似乎是不允许的

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

我该怎么做?

1 个答案:

答案 0 :(得分:2)

我认为您可以在班级指定第二个索引:

@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;
}