JPA:将默认列值设置为数据库中的序列

发布于 2024-10-09 13:44:59 字数 653 浏览 0 评论 0原文

我使用 Postgres 作为数据库,使用 JPA 2.0 作为提供程序。

我一直在使用 @GenerateValue(strategy = GenerationType.IDENTITY) 它为该列生成自己的序列并执行此查询(或类似的)

ALTER TABLE "comment" ALTER COLUMN id SET DEFAULT nextval('comment_id_seq'::regclass);

现在我正在使用

@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "GEN_COMMENT")
@SequenceGenerator(name = "GEN_COMMENT", sequenceName = "SEQ_COMMENT")

它也生成它自己的序列但不' t 执行该查询。

我的观点是,当我使用 IDENTITY 时,数据库可以自动插入序列中的列值,这在除通过应用程序之外对数据库进行其他访问时很有帮助。

所以我的问题是,是否有任何方法可以强制 JPA 将该列的默认值设置为序列而不重新定义 @Column(columnDefinition="...")

多谢。

I am using Postgres as database and JPA 2.0 as provider.

I had been using @GeneratedValue(strategy = GenerationType.IDENTITY) which generates its own sequence for that column and also executes this query ( or similar )

ALTER TABLE "comment" ALTER COLUMN id SET DEFAULT nextval('comment_id_seq'::regclass);

Now I am using

@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "GEN_COMMENT")
@SequenceGenerator(name = "GEN_COMMENT", sequenceName = "SEQ_COMMENT")

which also generates it's own sequence but doesn't execute that query.

My point is that when I had been using IDENTITY then database could automatically insert the column value from sequence which is helpful when there are other accesses into database except via application.

So my question is if there is any way how to force JPA to set default value of that column to sequence without redefinition of @Column(columnDefinition="...")?

Thanks a lot.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

慈悲佛祖 2024-10-16 13:44:59

这是特定于数据库的,Postgres 已将类型 serial 定义为生成类型 IDENTITY,它会自动创建此数据库结构。

This is database specific, the Postgres has defined type serial as generation type IDENTITY which automatically creates this database structure.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文