【发布时间】:2013-06-12 16:35:06
【问题描述】:
我只是在 PostgreSQL 上尝试使用 Dropwizard 和 Hibernate 的 Liquibase,所有最新版本,但我遇到了障碍。我打算为我的类 ID 使用 Hibernate 的 UUID 生成器,它会生成一个字符串 PK。我指定我想要一个名为id 的varchar(32) 字段在我的表中,但Liquidbase 生成了bigint 字段。然而有趣的是,这两个表有不同的列:portfolio.id 有序列portfolio_id_seq,而 Stock 没有序列。
我是不是做错了什么,或者这只是 Liquibase 的 ID 列的一些奇怪属性?我应该使用bigints 吗?代码如下:
migrations.xml:
<changeSet id="1" author="orlade">
<createTable tableName="stock">
<column name="id" type="varchar(255)">
<constraints primaryKey="true" nullable="false" />
</column>
<column name="name" type="varchar(255)">
<constraints nullable="false" />
</column>
<column name="symbol" type="varchar(255)">
<constraints nullable="false" />
</column>
<column name="description" type="varchar(255)" />
</createTable>
<createTable tableName="portfolio">
<column name="id" type="varchar(255)">
<constraints primaryKey="true" nullable="false" />
</column>
<column name="name" type="varchar(255)">
<constraints nullable="false" />
</column>
<column name="description" type="varchar(255)" />
</createTable>
</changeSet>
Portfolio.java:
@Entity
@Table(name = "portfolio")
public class Portfolio {
@Id
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid")
private String id;
}
Stock.java:
@Entity
@Table(name = "stock")
public class Stock {
@Id
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid")
private String id;
}
【问题讨论】:
-
可能没有帮助,但 UUID 不应该是正确使用的类型吗?
-
好吧,如果你存储 uuid,你不使用序列生成器,你可以在添加
ossp-uuidcontrib 模块后设置DEFAULT uuid_generate_v4(),或者让你的应用程序生成它们。听起来您的 ORM 没有收到您想要 UUID 的消息。 -
确实,如果我使用 UUID,我想我也应该让 Hibernate 知道。这样做之后,它仍然创建了 bigint 列。
标签: database postgresql database-schema liquibase dropwizard