简体   繁体   中英

hibernate ID increment oracle

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

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!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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