Custom message error for an unique-constraint

Hello ,

I use an unique constraint like this in my entity
<unique-constraint columns="name"/>

when i create a seconde object with the same name i have this message :

Unique constraint violation
The record(s) can’t be updated as it violates unique constraint.

ERROR: duplicate key value violates unique constraint “uk_kultdwsasgo9wha9o3eyqrld”
Détail : Key (name)=(blabla) already exists.

I think this message is not readeable for a user. Do you have some idea where i can change this message ?

Thx,

Hi,

I don’t think you can override JPA messages but maybe you have another way to do it with an using custom error and condition you can call with "OnSave=“my_action,save” on your form. On your condition you can check if another object as the same value and if yes show the custom message.

More complicated: you can create your onwn module with a service and a controller that could make the stuff.

Regards,

/**
   * call check record duplicate, use to check one-to-many property filed
   *
   * @param request
   * @param response
   */
  @SuppressWarnings("unchecked")
  public void callCheckOneToManyDuplicate(ActionRequest request, ActionResponse response) {
    LOG.debug("Call check duplicate for model : {} ", request.getModel());

    String oneToManyFieldName = (String)request.getContext().get("oneToManyFieldName");
    Map parent = (Map)request.getContext().get("_parent");
    List<HashMap> itemList = (List<HashMap>)parent.get(oneToManyFieldName);
    Class<? extends Model> modelClass = (Class<? extends Model>)JPA.model(request.getModel());
    if(itemList != null && itemList.size() > 0){
      try{
        for (UniqueConstraint uniqueConstraint : ModelTool.getUniqueConstraints(modelClass)) {
          Set<Map<String, Object>> set = new HashSet<Map<String, Object>>();
          String fieldString = " ";
          for(HashMap item : itemList){
            Map<String, Object> map = new HashMap<String, Object>();
            for(String fieldName : uniqueConstraint.columnNames()){
              Field field = modelClass.getDeclaredField(fieldName);
              Object value = item.get(fieldName);
              if(value instanceof Map){
                value = ((Map)value).get("id");
              }
              map.put(fieldName, value);
              fieldString = fieldString + field.getDeclaredAnnotation(Widget.class).title() + " ";
            }
            set.add(map);
          }
          if (set.size() != itemList.size()) {
            response.setError(fieldString + "值重复");
          }
        }
      }catch (NoSuchFieldException e){
        TraceBackService.trace(e);
      }
    }

  /**
   * Get unique constraint array of fields affected by unique constraint.
   *
   * @param clazz
   * @return
   */
  public static UniqueConstraint[] getUniqueConstraints(Class<? extends Model> clazz) {
    Table annotation = (Table) clazz.getAnnotation(Table.class);
    if (annotation != null) {
      return annotation.uniqueConstraints();
    }
    return new UniqueConstraint[0];
  }