Nicolas Constanzo
Nicolas Constanzo

Reputation: 3

Java Hibernate Id Question

Is there any way I could generate a four digit number from the AutoGenerated Id that the entity creates? For example: Person has a unique #1. And I would like to create another number that looks like 1001. Person #130 would be 1130. I don't think my table is going to be over 9999.

Any help would be greatly appreciated. Thanks in advance, Nick

Upvotes: 0

Views: 95

Answers (2)

mavroprovato
mavroprovato

Reputation: 8362

You can create another method inside the entity class that uses the generated Hibernate id, like:

public String getNumber() {
    return "#" + Integer.toString(1000 + this.id);
}

The only problem is that you have to save the object first, before this method returns a meaningful value.

Upvotes: 0

JB Nizet
JB Nizet

Reputation: 691735

Use a sequence generator and make the sequence start at 1000.

Upvotes: 2

Related Questions