krionz
krionz

Reputation: 447

How to specify order of fields inside a composed primary key?

This question is about ordering inside a composed index, which is a more specific problem than ordering columns inside a table.

I have a simple class with a composed id:

@Embeddable
class PrimaryKeyClass {
    private int a;
    private int b;
}

@Entity
class SimpleClassWithComposedId {
    @EmbeddedId
    PrimaryKeyClass id;
}

It seems like hibernate is generating the primary key with the fields ordered by lexicographical order, at least it was what happened in my tests cases. But i would like to know if there is a way to specify the order of the fields in the primary key, because this is very important since we can heavily optmize queries that use a prefix of the composed index ( i have read this in the postgres docs )

Upvotes: 3

Views: 524

Answers (1)

Thiyanesh
Thiyanesh

Reputation: 2360

One option would be to rename the columns in the table in the lexical order.

Alternatively, does adding a Column name help?

@Column(name = "a")
private int k_2a;

@Column(name = "b")
private int k_1a;

Upvotes: 1

Related Questions