如何在 EclipseLink + PostgreSQL 中为 GenerationType.AUTO id 字段创建列 + 序列?

How to create columns + sequences for GenerationType.AUTO id fields in EclipseLink + PostgreSQL?

我正在使用 EclipseLink 2.5.2。我有这个包含生成的 id 的抽象实体:

@GeneratedValue(strategy = GenerationType.AUTO)
@Id
private Long id;

我希望我的应用程序创建或扩展我的 (PostgreSQL 9.3.7) 数据库,所以我在 persistence.xml:

中使用这些属性
<properties>
  <property name="eclipselink.ddl-generation" value="create-or-extend-tables"/>
  <property name="eclipselink.ddl-generation.output-mode" value="both"/>
</properties>

在应用程序 运行 之后,表被创建,但是如果没有在我的插入查询中显式设置 id,我无法使用普通 SQL 插入数据。所以,生成的价值策略并没有体现在数据库模型中。

它是这样创建的:

id bigint NOT NULL

但我希望它是这样的:

id bigint NOT NULL DEFAULT nextval('some_generated_sequence'::regclass)

当我连接到 Derby 数据库时,自动生成的 ID 确实 起作用(生成了一个序列)。

如何配置 EclipseLink 以使用 PostgreSQL 数据库为 GenerationType.AUTO id 字段正确生成数据库列和序列?

我还尝试定义一个序列并在我的 id 字段上使用它:

@MappedSuperclass
@SequenceGenerator(name="global_sequence", sequenceName="global_sequence", allocationSize=1, initialValue=1)
public abstract class AbstractEntity implements Serializable
{
  @Id
  @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "global_sequence")
  @Column(nullable = false)
  private Long id;

  ...
}

当我 运行 我的应用程序出现此错误时:

Internal Exception: org.postgresql.util.PSQLException: ERROR: relation "global_sequence" does not exist

generationtype.auto 允许提供者选择使用的序列类型,在这种情况下,EclipseLink 可能选择使用允许预分配的序列 table。如果要使用特定类型的序列生成,则应指定数据库支持的一种,但要注意权衡,因为身份类型排序需要在插入语句后获取值,这可能会限制性能选项的使用,例如批量插入和预分配。