0

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".
Boermt-die-Buse
  • 94
  • 1
  • 3
  • 12
  • Have you tried using flush() method before saving from the session object? – Lucky Aug 28 '15 at 11:39
  • i tried it, flush() has no effect. Also i think hibernate uses flush() automatically when transaction.commit() is called. – Boermt-die-Buse Aug 28 '15 at 11:47
  • Okay, Try `session.merge(object)` to save your data. – Lucky Aug 28 '15 at 11:54
  • @JordiCastilla my new configuration gives me the exception "NonUniqueObjectException". I don't know for sure if i get id's(23,24) but i have the new configuration for every table and the other empty tables start all with ID = 1. – Boermt-die-Buse Aug 28 '15 at 12:13
  • i am currently trying to do your solution, but sql developer is saying that there is an syntax error. And i try to get rid of that, thats why am not responding – Boermt-die-Buse Aug 28 '15 at 12:18
  • What oracle version? – Grim Aug 31 '15 at 12:45
  • @Peter Rader Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production – Boermt-die-Buse Sep 01 '15 at 08:36
  • @JordiCastilla can you please upvote my question again, i assume you donvoted it and i find it not fair that you are deleting your wrong downvoted answers just to have a good reputation. I solved my problem and posted the solution so anyone can solve their problems. But if my question is downvoted it seems like my questions makes no sense. Thank you in advance. – Boermt-die-Buse Sep 01 '15 at 12:20

1 Answers1

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

Community
  • 1
  • 1
Boermt-die-Buse
  • 94
  • 1
  • 3
  • 12