Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I specify the order of columns within a multi-column index in Hibernate?

I'm creating a multi-column index as follows:

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

However I can't see how to control the order with which the three columns appear within the composite index (which can be required to ensure optimal query performance). How can this be achieved?

like image 874
sanity Avatar asked Jan 31 '26 19:01

sanity


1 Answers

This can be specified with the org.hibernate.annotations.Table annotation, which is used in addition to the javax.persistence.Table annotation:

@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;
}
like image 54
toohool Avatar answered Feb 02 '26 10:02

toohool