How to automatically generate UUID on Object Save

Hello, I am actually developing an OpenMRS module that exposes via OpenMRS Rest some services to perform an action to some tables I added in. The problem is that, when I save an object using the POST method, the UUID is empty. Could someone help me on how I could automatically generate UUID when creating a new item in my database table? Thanks

How are you saving an object, Is this be of help How To Use UUIDs With Hibernate And MySQL

@sharif I am using Liquibase and Hibernate hbm files. And I am using a DAO strategy for database operations

The right thing would be to make your data model class extends BaseOpenmrsObject or, better still, whichever of BaseOpenmrsData or BaseOpenmrsMetadata makes the most sense for your object.

However, if you really just want to populate the uuid field, then you can just do what we do in BaseOpenmrsObject, i.e. default your UUID field to a random UUID:

private String uuid = UUID.randomUUID().toString();
2 Likes