Java Hibernate 和 Oracle 序列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19582219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 18:31:38  来源:igfitidea点击:

Hibernate and Oracle Sequence

javaoraclehibernate

提问by Vlad

I have a problem with Oracle sequence and Hibernate. I used this code to get Oracle Sequence with hibernate

我有 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;
}

but when i insert a new value to the table, the generated value is incorrect. For example: when I had two records in database with id 2 and 3, and when I inserted new one, it's id was not 4 but 25. I have no idea what to do with it.

但是当我向表中插入一个新值时,生成的值不正确。例如:当我在数据库中有两条记录,id 为 2 和 3,当我插入一条新记录时,它的 id 不是 4,而是 25。我不知道如何处理它。

回答by Przemyslaw Kruglej

The reason is that probably your sequence has a CACHE value specified during its creation. This has been asked a few times already, please check the two following links:

原因可能是您的序列在创建过程中指定了一个 CACHE 值。这已经被问过几次了,请检查以下两个链接:

Sequence gaps in Hibernate

Hibernate 中的序列间隙

Sequence gaps - Oracle

序列缺口 - Oracle

回答by Ilya

You should set allocationSizeto 1

你应该设置allocationSize1

@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "student_id_seq")
@SequenceGenerator(name = "student_id_seq", 
                   sequenceName = "Student_seq",
                   allocationSize = 1)  

You can read more in documentation SequenceGenerator doc

您可以在文档SequenceGenerator doc 中阅读更多内容

回答by gvo

When I see your question, I wonder : Do you really need to have the id 4 instead of 25, or is it just a technical primary key? Behind the cache issue, if you ask a value from your sequence (id=4), and then rollback your transaction, the next time you'll ask a number you will have a gap (id=5), behind any hibernate or cache-related issue.

当我看到你的问题时,我想知道:你真的需要 id 4 而不是 25,还是只是一个技术主键?在缓存问题背后,如果您从序列中询问一个值 (id=4),然后回滚您的事务,下次您询问一个数字时,您将有一个间隙 (id=5),在任何休眠或缓存之后-相关问题。

As stated in Przemyslaw's second link, Sequence gaps - Oracle: "You should never count on a sequence generating anything even close to a gap free sequence of numbers. They are a high speed, extremely scalable multi-user way to generate surrogate keys for a table."

正如 Przemyslaw 的第二个链接Sequence gaps - Oracle 所述:“您永远不应该指望一个序列生成任何甚至接近无间隙数字序列的东西。它们是一种高速、高度可扩展的多用户方式,可以为桌子。”

If, as I understand, there is no need to have contiguous value in your application, the good answer to the "what to do with it" question is : nothing, just live with those gaps.

根据我的理解,如果您的应用程序中不需要具有连续的价值,那么“如何处理它”问题的最佳答案是:什么都没有,只是忍受这些差距。

回答by bedjaoui djounaydi

you should create in your database the sequence like:

您应该在数据库中创建如下序列:

CREATE SEQUENCE  "Student_seq"  MINVALUE 0 MAXVALUE 1000000000 INCREMENT BY 1 START WITH 1 CACHE 500 NOORDER  NOCYCLE ;

and in your student.hbm.xmlconfiguration make :

并在您的student.hbm.xml配置中 make :

<class name="StudentPersistant" table="Student">

<id  name="id"  type="java.lang.Long" column="StudentID" >
      <generator class="sequence"> 
           <param name="sequence">Student_seq</param>   
      </generator>
</id>