开发者

Generate ID's above a certain value

开发者 https://www.devze.com 2023-02-18 05:28 出处:网络
In my JPA+Hibernate+MySQL app I have an entity like so: @Entity public class Room { @Id @GeneratedValue private long id;

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 (开发者_Go百科I don't know who actually does it now) so that ID's are always higher than a certain value, e.g. 100000000?


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;


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消