hibernate ID increment oracle

独自空忆成欢 提交于 2019-12-23 05:29:10

问题


I managed to let hibernate increment the id of every table by 1 by putting this into every entity class.

@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. 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. For instance, table testchamber has 22 rows with data. 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.

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. For instance, hibernate gave a new entity the id 60, 61 instead of 23, 24 and so on..

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".

回答1:


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. 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. Thank you very much @nolexa!!



来源:https://stackoverflow.com/questions/32270146/hibernate-id-increment-oracle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!