gpt4 book ai didi

java - JPA 非主键字段自动递增

转载 作者:行者123 更新时间:2023-11-30 07:34:46 24 4
gpt4 key购买 nike

是否可以自动递增非 PrimaryKey 的 JPA 字段?

我已经看到类似创建一个只有一个自动递增的 id 的新实体并在我想要的字段上使用它之类的东西,但我不知道如何制作它。

最佳答案

不可以,不幸的是,@GenerateValue 注释只能在主键字段上使用。

来自文档

The GeneratedValue annotation may be applied to a primary key property or field of an entity or mapped superclass in conjunction with the Id annotation. The use of the GeneratedValue annotation is only required to be supported for simple primary keys. Use of the GeneratedValue annotation is not supported for derived primary keys.

我不确定您想要做什么,但或者,您可以使用 native 查询从序列中获取下一个值,并在您的字段中设置该值。我确实认为,无论您尝试做什么,都会有另一种不使用序列的方法......如果您分享问题,其他人可能能够提出解决方案。

关于java - JPA 非主键字段自动递增,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35559387/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com