Reputation: 230
I am not sure what happened but one day Hibernate started to wrong generate auto ID in Osoba.
What I mean is when I add person(Osoba) he gets ID 60, next one gets 64 but it should get 61... How can I fix that? So it will increment by 1 no by 4 or other like this is now... Also how can I "reset" ID value? I would like to clean whole table, and start counting from ID 1
<class name="Osoba" table="DANEOSOBOWE">
<id name="id" type="int" column="id">
<generator class="native"/>
</id>
public class Osoba implements Interface {
private int id;
private String imie;
private String nazwisko;
private String email;
private String telefon;
private String uczelnia;
private String doswiadczenie;
private String skadSlyszal;
private List zainteresowania = new ArrayList();
public Osoba(){ // domyslny
}
// konstruktor zeby mozna bylo sobie w jednej linijce dodawac osobe
public Osoba(String imie1, String nazwisko1, String telefon1, String email1, String uczelnia1, String doswiadczenie1, String skadSlyszal1 ){
this.imie = imie1;
this.nazwisko = nazwisko1;
this.email = email1;
this.telefon = telefon1;
this.uczelnia = uczelnia1;
this.doswiadczenie = doswiadczenie1;
this.skadSlyszal = skadSlyszal1;
}
Upvotes: 2
Views: 2942
Reputation: 2655
Native: This generation strategy is the default. It simply chooses the primary key generation strategy that is the default for the database in question, which quite typically is IDENTITY
, although it might be TABLE
or SEQUENCE
depending upon how the database is configured. The native strategy is typically recommended, as it makes your code and your applications most portable.
For example: In Mysql if you have primary key column as auto_increment
, the db will be updated using this strategy.
Upvotes: 2
Reputation: 64
I had the same problem, but increment was 32 instead of 4 as for you: I solved it by closing sessionFactory
. I'm using H2 database, so here is link to documentation:
http://www.h2database.com/html/grammar.html#set_cache_size
Where it says:
The cache is the number of pre-allocated numbers. If the system crashes without closing the database, at most this many numbers are lost. The default cache size is 32. To disable caching, use the cache size 1 or lower.
So when I didn't close sessionFactory
in my Main
method, the increment was 32, but when I started closing it, it worked. I re-created database, and it worked. And when I looked to INFORMATION_SCHEMA.SEQUENCES
table, which is the table with properties of sequence generation of my database , I saw that magic CACHE_SIZE = 32
.
So every time database was not closed properly, the sequence lost exactly 32 numbers.
Upvotes: 1
Reputation: 426
Using annotations -
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="course_seq")
@SequenceGenerator(
name="course_seq",
sequenceName="course_sequence",
allocationSize=1
)
private int id;
allocation size of 1 will increment it by 1.
Upvotes: 2