【问题标题】:Whether we can use auto increment as well as manual insertion in hibernate for primary key我们是否可以在hibernate中使用自动增量以及手动插入主键
【发布时间】:2018-10-08 13:56:20
【问题描述】:

我有主键列的场景,应该发生自动增量。有时我必须手动设置。在休眠中它可能吗?

如果可能,请帮助我应该使用什么注释。

【问题讨论】:

  • 为什么有时必须手动设置,而其他时候则不需要?你可以设置你想使用的Id,是的。
  • 好的,我会那样做的。

标签: hibernate


【解决方案1】:

是的,如果您使用strategy = GenerationType.SEQUENCE,您可以随时更改数据库中的序列。 在下面的代码中,我创建了序列instructor_id_sequ

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "instructor_id_generator")
@SequenceGenerator(name = "instructor_id_generator", allocationSize = 1, initialValue = 101, sequenceName = "instructor_id_sequ")
@Column(name="INSTRUCTOR_ID")
private int instructorId;

以下是您可以在 DB 中检查的序列,并且可以重新创建或更改相同的序列。 [我用的是Oracle10]

【讨论】:

    猜你喜欢
    • 2013-11-05
    • 1970-01-01
    • 1970-01-01
    • 2013-11-12
    • 1970-01-01
    • 1970-01-01
    • 2011-06-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多