Create UUID based in composite key


Is it possible to generate the UUID via an MD5 function on the composite key of an entity?
If I have a person entity with NAME and SURNAME as ‘natural’ key and I want to use the md5 of the concatenated fields as UUID.



Have you already googled something like “convert md5 to uuid”? If so, what is the actual question?

And please be careful with natural keys. In your case, you will not be able to save changes in the entity if you modify NAME or SURNAME.


yes, you can do that. Here is an example:

You can create a class that will create MD5 based UUIDs from natural keys like this (groovy based implementation):

class CompositeUuidCreatorBean implements CompositeUuidCreator {

    UUID createCompositeUuid(String... naturalKeys) {
        String md5UuidString = createMd5UuidString(naturalKeys)

    private String createMd5UuidString(String... naturalKeys) {
        String joinedNaturalKeys = naturalKeys.join('')

    private UUID addHyphenToUuidString(String uuidStringWithoutHyphen) {
                '-' + uuidStringWithoutHyphen[7..11]+
                '-' + uuidStringWithoutHyphen[11..15]+
                '-' + uuidStringWithoutHyphen[15..19]+
                '-' + uuidStringWithoutHyphen[19..-1])

In order to use it in a CUBA app, you can use a EntityListener that does the wiring:

public class CustomerIdGeneratorEntityListener implements BeforeInsertEntityListener<Customer> {

    CompositeUuidCreator uuidCreator;

    public void onBeforeInsert(Customer entity, EntityManager entityManager) {
        UUID entityId = uuidCreator.createCompositeUuid(entity.getName(), entity.getSurname());


With this the ID gets written before the insert. I created a whole CUBA example for you that you can find attached.

Note, in my implementation i ensured that i can’t update the name and the surname through the controller. That might not be the best position to do this thing. But anyway - you obviously can’t change the name and the surname anymore since they are part of the identifier.

Mario (33.0K)

Hi Mario,

Thank you. This is exactly what I needed :slight_smile:
It is not a problem that the ‘natural key’ cannot be changed. In the rare cases it has to be changed we mark the old one as deleted and insert the new one with a reference to the old one.
Then we can stil find the old key if needed.

I’ll build a service that does de key renames. Then I can use JQL to implement the cascading changes.