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