How to return notification to user when validation (uniquness of field) fails


I have added the annotation @uniquness=true to an entity field to avoid none unique entries in a database colum. On button click event, when writing from textfield, which contains a none unique value, to database, the binder.WriteBean method shows no issue, but when save to repository an error is thrown stating “o.h.engine.jdbc.spi.SqlExceptionHelper : (conn=290) Duplicate entry ‘123’ for key ‘UK_1yqs95vd5s670qf7n91154bit’” which works as expected. Now I want to avoid throwing the error message in log, but want to inform the user about the textfield, which contains the none unique content. Honestly I don’t know how to realize this using validation.

Any help would be really appreciated.

thanks a lot in advance, best regards, Michael

It depends on your requirements, when do you have to notify the user about the conflict:

  • while changing this specific text field? Add a validation that calls your repository / service and check that the new value wasn’t already stored (keep in mind that people could use this as a way spoof all possible values from your database easily)
  • or do the same check described above after the writeBean method before you store it in the database

Hi @quirky-zebra , thanks for your reply. I would like to check on button click event when inputs from textfields should be stored in database, so I would see it as your second case. Thanks also for your hint with respect to spoof of values, but in my case the content should be unique, but it is not secret or critical value. I have a bit more elaborated on this topic and, as the field as been annotated as unique, the column is also marked unique in db, so same value will not be excepted twice. I now got the error message I mentioned in my previous post and present it as notification to users, but I am not sure, it is a “good” way:
try { binder.writeBean(this.loginuser); try{ loginUserService.update(this.loginuser);"Login User Details saved!", 3000, Notification.Position.MIDDLE); }catch (Exception ex){ if (ex.getCause().getCause() instanceof SQLException) { SQLException e1 = (SQLException) ex.getCause().getCause();"Exeption during save operation: " + e1.getMessage(), 3000, Notification.Position.MIDDLE); } } } catch (ValidationException ex) {"An exception happened while trying to store new Login User Details!", 3000, Notification.Position.MIDDLE); }
Thanks a lot in advance , Best regards, Michael

I would create a new method in your LoginUserService like “isValueAlreadyStored(string userinput)” that checks if the duplicated value is already in the database and if that query returns a result you can show a error notification that the value is already in use, instead of doing the persisting action. Yes that’s not 100% safe if in between the check and the storage something happens in 12 nano seconds but that error margin is really low :sweat_smile:

Hi @quirky-zebra , yes, that’s right, it is more straight forward. And according to my app, there is nothing happening in 12 nano seconds :slightly_smiling_face: :slightly_smiling_face:. thanks again for your feedback.