I am trying to add a validation to my entity disallowing the user to enter duplicate entries (based on some fields combination).
For this, I have created an entity listener and implemented the 2 methods onBeforeInsert() and onBeforeUpdate(), which read existing data from the database.
Based on that, I want to display an error message to the user if he is trying to enter duplicate info.
What is the best way to do this? Could you please share by providing some pseudo-code or an example?
I have read some related questions on the forum as well as the part related to Exception Handling in the guide, but couldn’t really manage to understand where to define the exception and how to call it, use it and throw it.
if i get it correctly, this requirement should be solvable through the usage of a unique constraint on the database that combines all the fields of your entity. A unique constraint can be configured through studio (see attached screenshot).
I created the unique constraint on the entity level, but I am failing to customize the error message.
I have added the unique contraint name in messages.properties (tried all messages.properties fiels under web, under gui, …) but always getting “unique constraint violation occurred”. I am not sure what I am missing. I tried restarting the app server with no luck.
Any suggestion?
As for the second part related to the entity listeners, I understand it is not needed for this unique validation specifically, but I need to implement many validations in my billing application soon.
I will be reviewing the example you shared and get back to you.
As for the second part related to the entity listeners, I understand it is not needed for this unique validation specifically, but I need to implement many validations in my billing application soon. I will be reviewing the example you shared and get back to you.
As a side note, in 6.4 (at the end of the month) there will be a new mechanism using BeanValidation that might change the way how validation can be done. I just wanted to point that out, not that you do this implementation twice
I’ve just added examples of specifying a unique constraint message and handling an error message from an entity listener to the Data Manipulation sample.