【发布时间】:2013-11-04 02:55:25
【问题描述】:
我对 Oracle 序列和 Hibernate 有疑问。我使用此代码通过休眠获取 Oracle 序列
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "student_id_seq")
@SequenceGenerator(name = "student_id_seq", sequenceName = "Student_seq")
@Column(name = "StudentID")
public Long getStudentId() {
return this.studentId;
}
public void setStudentId(Long studentId) {
this.studentId = studentId;
}
但是当我向表中插入一个新值时,生成的值不正确。例如: 当我在数据库中有两条 id 为 2 和 3 的记录时,当我插入新的记录时,它的 id 不是 4 而是 25。我不知道该怎么处理它。
【问题讨论】:
-
使用 'strategy = GenerationType.AUTO' 而不是 'strategy = GenerationType.SEQUENCE'。
-
为什么要这样做? (顺便说一句,我已经尝试过了,但没有运气)