简体   繁体   English

休眠ID增量oracle

[英]hibernate ID increment oracle

I managed to let hibernate increment the id of every table by 1 by putting this into every entity class. 我设法通过将每个表的ID放入每个实体类,使休眠状态将每个表的ID递增1。

@Entity
public class TestObject implements Serializable{

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator="idgen")
@SequenceGenerator(
        name="idgen",
        sequenceName="testobject_seq",
        allocationSize=1,
        initialValue=1
    )

I have two tables which i fill with data manually through the Oracle SQL Developer. 我有两个表,这些表通过Oracle SQL Developer手动填充数据。 After i created the tables. 创建完表格后。

<property name="hibernate.hbm2ddl.auto">create</property>

For the 2 tables with data in it i set the initialValue to what i need. 对于其中有数据的2个表,我将initialValue设置为所需的值。 For instance, table testchamber has 22 rows with data. 例如,表testchamber具有22行数据。 So my annotation changes to this: 所以我的注释变为:

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator="idgen")
@SequenceGenerator(
        name="idgen",
        sequenceName="testchamber_seq",
        allocationSize=1,
        initialValue=23
    )

But when i try to save a new testChamber entity i get this error. 但是,当我尝试保存新的testChamber实体时,出现此错误。

org.hibernate.NonUniqueObjectException: A different object with the same identifier value was already associated with the session

I could save my entity without a problem before i changed the annotation but with the old annotation hibernate incremented the id randomly. 在更改注释之前,我可以保存我的实体而不会出现问题,但是使用旧注释时,hibernate会随机增加id。 For instance, hibernate gave a new entity the id 60, 61 instead of 23, 24 and so on.. 例如,休眠给新实体id 60、61,而不是23、24,依此类推。

This was my old annotation: 这是我的旧注释:

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
  • Is there a correct way, to tell hibernate that a table has already data in it and it should start to count form a specific number? 有没有一种正确的方法来告诉休眠表中已经有数据,并且它应该从一个特定的数字开始计数?
  • or how can i get rid of the "NonUniqueObjectException". 或如何摆脱“ NonUniqueObjectException”。

This post helped me with my problem " link" . 这篇文章帮助我解决了问题“ 链接” But the anser from @nolexa is correct, not from Alex Gitelman which is actually checked as correct. 但是@nolexa的回答是正确的,而不是来自Alex Gitelman的,实际上被检查为正确的。 I put this 我把这个

<property name="hibernate.id.new_generator_mappings">true</property>

into my hibernate.cfg.xml file and all my created sequences work fine now. 放入我的hibernate.cfg.xml文件中,所有创建的序列现在都可以正常工作。 Thank you very much @nolexa!! 非常感谢@nolexa!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM