如何在Hibernate中指定多列索引中的列顺序?

时间:2011-01-26 00:52:13

标签: java hibernate jpa

我正在创建一个多列索引,如下所示:

@Entity
public class Ranking extends Model {
    @ManyToOne
    @Index(name = "ranking_ix")
    public Rankable rankable;

    @ManyToOne
    @Index(name = "ranking_ix")
    public Criteria criteria;

    @Index(name = "ranking_ix")
    public double rank;
}

但是,我无法看到如何控制三列在复合索引中出现的顺序(这可能是确保最佳查询性能所必需的)。如何实现这一目标?

2 个答案:

答案 0 :(得分:2)

这可以使用org.hibernate.annotations.Table注释来指定,除了 javax.persistence.Table注释之外还使用

@Entity
@javax.persistence.Table(name="Ranking")
@org.hibernate.annotations.Table(
    appliesTo="Ranking",
    indexes = { @Index(name="ranking_ix", columnNames = { "rankable", "criteria", "rank" } ) }
)
public class Ranking extends Model {
    @ManyToOne
    public Rankable rankable;

    @ManyToOne
    public Criteria criteria;

    @Index(name = "ranking_ix")
    public double rank;
}

答案 1 :(得分:-1)

尝试使用符号

@Entity
@Table(
    name="Ranking",
    indexes = { @Index(name="ranking_ix", columnNames = { "Rankable", "Criteria" } ) }
    )
public class Ranking extends Model {
    @ManyToOne
    public Rankable rankable;

    @ManyToOne
    public Criteria criteria;

    @Index(name = "ranking_ix")
    public double rank;
}