4

I use Spring Data JPA and PostgreSQL 10.

Given the classes

@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()
}

and

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

In the DB the table is created by hibernate via

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

I want PRIMARY KEY (a, b) instead of PRIMARY KEY (b, a) for performance reasons. How can I achieve this without renaming the columns?

Johannes Flügel
  • 3,112
  • 3
  • 17
  • 32
  • 1
    The JPA spec doesn't allow a mechanism for defining the order of columns in schema definition. Some JPA providers (e.g DataNucleus) allow an extension to do that. No idea about your provider. You could just define the schema yourself. –  Mar 21 '18 at 11:30

1 Answers1

0

Try @Embeddable for composite keys

@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;
}
ABHAY JOHRI
  • 1,997
  • 15
  • 19
  • It doesn't help. Even if I swap a and b I will get `CONSTRAINT test_table_pkey PRIMARY KEY (a, b)` instead of `... PRIMARY KEY (b, a)`. – Johannes Flügel Mar 23 '18 at 13:38