Can we generate primary keys automatically without JDBC queries or calls?

I am trying to automatically generate alpha-numberic primary keys (e.g. TESLA1001) in Hibernate. I am currently using an Oracle database, so I have a JDBC call to my_sequence.NEXTVAL (1002) to increase the number and add to the prefix (TESLA).

We consider MySQL as an option, but they do not support sequences. Therefore, I am forced to rewrite the method of generating a user identifier by calling JDBC in a stored procedure.

Is there any way to create a common implementation to create custom primary keys without using JDBC queries and databases? So, in the future, if I need to test my application using MSSQL, I only need to change my hiberate configuration and everything will work fine!

+5
source share
1 answer

Since you need a way to coordinate the sequence number, you always have to use a generator with a centralized sequence. An alpha-numeric primary key will perform worse during indexing than a UUID generator.

If I were you, I would switch to UUIDs that are unique and portable across all major RDBMSs.

+1
source

All Articles