Boermt-die-Buse
Boermt-die-Buse

Reputation: 94

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)

Upvotes: 0

Views: 2624

Answers (1)

Boermt-die-Buse
Boermt-die-Buse

Reputation: 94

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

Upvotes: 1

Related Questions