Bart van Heukelom
Bart van Heukelom

Reputation: 44124

Generate ID's above a certain value

In my JPA+Hibernate+MySQL app I have an entity like so:

@Entity
public class Room {

    @Id
    @GeneratedValue
    private long id;

    // columns....
}

This works perfectly fine, but the generated id values are, as expected, 1, 2, 3, etc. However, users are going to see this ID, and the client wants the id's to be longer (because he finds it looks nicer), from the day of the application release.

How can I change the ID generation of Hibernate or MySQL (I don't know who actually does it now) so that ID's are always higher than a certain value, e.g. 100000000?

Upvotes: 1

Views: 293

Answers (3)

Rich Hill
Rich Hill

Reputation: 463

generated value without a strategy defaults to "AUTO" and that means the database is providing the id value. You need to seed the column in MYSQL to provide the correct value

Upvotes: 1

J V
J V

Reputation: 11946

ALTER TABLE tbl AUTO_INCREMENT = 100000;

That should work. Although being a purely cosmetic change you should just change it on output (And input) so that the data isn't corrupted by PHB-syndrome

Upvotes: 0

Bozho
Bozho

Reputation: 597382

There are multiple ways. For example you can create a custom hibernate generator. But the easiest way is to simply tell MySQL where to start counting from:

ALTER TABLE tbl AUTO_INCREMENT = 100000;

Upvotes: 1

Related Questions