【问题标题】:JPA: how to change order of columns in primary key [duplicate]JPA:如何更改主键中列的顺序[重复]
【发布时间】:2018-08-30 11:44:34
【问题描述】:

我使用 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()
}

在数据库中,表是通过休眠方式创建的

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)。如何在不重命名列的情况下实现这一点?

【问题讨论】:

  • JPA 规范不允许在模式定义中定义列顺序的机制。一些 JPA 提供程序(例如 DataNucleus)允许扩展来执行此操作。不知道您的提供商。您可以自己定义架构。

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


【解决方案1】:

尝试使用@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;
}

【讨论】:

  • 它没有帮助。即使我交换 a 和 b,我也会得到 CONSTRAINT test_table_pkey PRIMARY KEY (a, b) 而不是 ... PRIMARY KEY (b, a)
猜你喜欢
  • 2011-12-29
  • 2016-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-10
  • 2020-12-29
  • 2017-03-10
相关资源
最近更新 更多