How to bind to a DTO

I’m able to bind to an entity class to a ComboBox. But the same is not working when the ComboBox is attached to a DTO. The binding is binder.bindInstanceFields(this); and the error says Property type 'com.example.application.entity.Unit' doesn't match the field type 'com.example.application.entity.dto.UnitDto'. Binding should be configured manually using converter.. Any samples to bind the DTO ?

It seems you are trying to set a field of type UnitDto with an object that is a Unit
You need to bind the field manually and set a coverter

I created a bindler like this binder.forField(unit) .withConverter(getUnitDtoUnitConverter()) .bind(Item_.UNIT);

and the converter is ```private static Converter<UnitDto, Unit> getUnitDtoUnitConverter() {
return new Converter<UnitDto, Unit>() {
@Override
public Result convertToModel(UnitDto unitDto, ValueContext valueContext) {
Unit unit1 = new Unit();
unit1.setId(unitDto.id());
unit1.setUnitName(unitDto.unitName());
return Result.ok(unit1);
}

        @Override
        public UnitDto convertToPresentation(Unit unit, ValueContext valueContext) {
            return unit.toUnitDto();
        }
    };
}```

and getting error as Caused by: java.lang.NullPointerException: Cannot invoke "com.example.application.entity.dto.UnitDto.id()" because "unitDto" is null at com.example.application.forms.ItemForm$1.convertToModel(ItemForm.java:171)

if I slightly modify this convertToModel like this @Override public Result<Unit> convertToModel(UnitDto unitDto, ValueContext valueContext) { if (unitDto == null) { return Result.ok(new Unit()); } Unit unit1 = new Unit(); unit1.setId(unitDto.id()); unit1.setUnitName(unitDto.unitName()); return Result.ok(unit1); } , getting a new error as ```Caused by: jakarta.validation.UnexpectedTypeException: HV000030: No validator could be found for constraint ‘jakarta.validation.constraints.NotEmpty’ validating type ‘com.example.application.entity.Unit’. Check configuration for ‘unit’
at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.getExceptionForNullValidator(ConstraintTree.java:116)
at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.getInitializedConstraintValidator(ConstraintTree.java:162)

This I’m able to fix because it is trying to validate NotEmpty on Unit which is not a String

so I removed that annotation

but the wholepoint of doing this is, I need the user to select an unit mandatorily. I can use asRequired but if so when do we need this converter ?

If the bean has a field of type dto and the UI component value is an entity, yes, you need to convert between the two types

in my case it is opposite. bean has a field of type entity and the UI component value is a dto and I want the user to provide a value mandatorily

If the types are different you need a converter