JPA:如何更改主键中列的顺序

时间:2018-03-21 11:17:57

标签: java postgresql hibernate jpa spring-data-jpa

我使用Spring Data JPA和PostgreSQL 10.

鉴于课程

@Entity
@Table(name = "test_table")
@IdClass(TestTableId.class)
public class TestTable {
    @Id
    private int b;
    @Id
    private int a;
    private int c;
    // Getters, setters, hashCode() and equals()
}

public class TestTableId implements Serializable {
    private int b;
    private int a;
    // Constructors, getters, setters, hashCode() and equals()
}

在数据库中,表由hibernate通过

创建
CREATE TABLE test_table
(
    a integer NOT NULL,
    b integer NOT NULL,
    c integer NOT NULL,
    CONSTRAINT test_table_pkey PRIMARY KEY (a, b)
)

出于性能原因,我想要PRIMARY KEY (a, b)而不是PRIMARY KEY (b, a)。如何在不重命名列的情况下实现此目的?

1 个答案:

答案 0 :(得分:0)

尝试使用@Embeddable复合键

@Embeddable
public class key implements Serializable {

    private static final long serialVersionUID = 1L;
    @Column(name = "a")
    private String a;
    @Column(name = "b")
    private String b;
}

public class App implements Serializable {

    private static final long serialVersionUID = 1L;
    @EmbeddedId
    private Key id;
}