Multiple Queries during insert

I’m doing a simple insert operation. Before I do the insert, I check if the value already exists. This is where I’m having issues. I can see the SELECT query is getting executed 4 times instead of one.
This is how I check for the existence of value

        boolean unitNameAlreadyExists = unitService.isUnitNameAlreadyExists(value);
        log.info("unitName {} AlreadyExists ? {}", value,unitNameAlreadyExists);
        return !unitNameAlreadyExists;
    }```

and the JPA method is ```@Query("select (count(u) > 0) from Unit u where upper(u.unitName) = upper(:unitName)")
    boolean existsByUnitNameIgnoreCase(@Param("unitName") String unitName);```

The binder is ``` private Binder.Binding<Unit, String> addUnitNameValidator() {
        log.info("Binding additional validations for unit name {}", unitName.getValue());
        Binder.Binding<Unit, String> unitNameValidator = binder.forField(unitName)
                .withValidator(s -> checkUnitNameAlreadyExists(s), "Unit name already exists")
                .bind(Unit_.UNIT_NAME);
        return unitNameValidator;
    }```

```  public void addAdditionalValidators() {
        if (binder.isValid()) {
            log.info("Adding validator for unit name");
            Binder.Binding<Unit, String> unitNameValidator = addUnitNameValidator();
            if (!binder.validate().getBeanValidationErrors().isEmpty()) {
                binder.removeBinding(unitNameValidator);
            }
        }
    }```.  What am  I doing wrong here and how to prevent the query running mulitple times ?
![image.png|1583x695](upload://gYDP0PHdi7X5UnQ4E2N29kcaSh9.png)

Don’t use a binder if you are worried for the amount of calls of the validation, that runs pretty often.

You can isUnitNameAlreadyExists when the field is updated by the user (e.isFromClient on addValueChangeListener) to give an immediate feedback and check it when the save is called.
That should be 2 queries in total when the unit name is updated.
And the code would be less complicated.
In your case your are validating the binder with binder.isValid() then revalidate the binder with binder.validate().