Create value for property in hibernation

I want to create a unique guid / uuid value for a property that is not the primary key in hibernation. In other words, I want to generate values ​​for an alternate primary key.

How can I do this using Hibernate?

+2


source to share


1 answer


The easiest way is to simply treat this alternate key like any other "normal" property in your pojo. Whenever you instantiate a new object, you just do something like

myinstance.setAlternateKey(UUID.randomUUID().toString());

      



If you want hibernate to automatically create a new UUID when you save a new object to the database, you could do so using hibernate interceptors and event listeners , especially PreInsertEventListener.

+1


source







All Articles