0

I am using JPA and Hibernate in my Java project, and I have an entity class with an id field that I want to generate as a shortened 21-character ID.

Currently, I am using the following annotations to generate a UUID for the id:

@Entity
public class MyEntity {

    @Id
    @GenericGenerator(name = "u_id", strategy = "org.hibernate.id.UUIDGenerator")
    @GeneratedValue(generator = "u_id")
    private String id;

}

However, I need the id to be shortened to 21 characters. I've researched and couldn't find a built-in solution for this specific requirement.

Is there any built-in method or a recommended approach to generate a shortened 21-character ID with JPA and Hibernate? If not, how can I implement a custom solution to achieve this? I want to ensure the uniqueness and avoid any potential collisions while generating these shortened IDs.

Any help or code examples would be greatly appreciated. If you find this question helpful or interesting, please consider upvoting it to support me and other developers who might have a similar concern. Thank you all in advance!

  • Does this answer your question? [how to reduce length of UUID generated using randomUUID( )](https://stackoverflow.com/questions/20994768/how-to-reduce-length-of-uuid-generated-using-randomuuid) – Mar-Z Jul 30 '23 at 18:20
  • `SecureRandom sr = new SecureRandom();byte[] a = new byte[11];sr.nextBytes(a);System.out.println(HexFormat.of().formatHex(a).substring(1));` is a possible approach if you're forced to generate it 'manually' – g00se Jul 30 '23 at 19:03
  • FYI… Your "shortened" UUID-as-text approach is actually taking *more* memory, not less. a Java `String` object containing 21 US-ASCII characters takes 336 bits (21 * 16). Likely similar in your database. Natively storing a complete UUID value takes only 128 bits. – Basil Bourque Jul 30 '23 at 21:40

1 Answers1

2

You can implement your own generator. Use org.hibernate.id.UUIDGenerator as an example of how to implement it.

The main problem is that I don't think it's possible to encode a UUID (128 bits) to 21 printable characters, so you'll need to implement an algorithm for the ID generation. You'll need to figure out what characters are valid for the generation and that will give you an idea of how many random bits you can use. As an example, if you can use all digits + upper/lower case ascii letters you can generate 21x6 bits and do a very basic mapping between 0-63 to one of those characters. Ideally this should include a checksum/parity character if there's a chance that the id will be entered manually.

Augusto
  • 28,839
  • 5
  • 58
  • 88
  • Could you please point me to an example that I can refer to for the same? – siddhartha agarwal Jul 30 '23 at 18:04
  • 1
    Sorry, I don't know one as this is a very specific requirement to you. Take a look at the implementation of the method `UUID.randomUUID` and see if you can figure out how to write a class that generates a 21-character identifier. – Augusto Jul 30 '23 at 18:16
  • 2
    Using Base85, you can encode 128 bits into 20 characters. – Codo Jul 30 '23 at 18:24